Dis-entabulation. No functional change.
[llvm-gcc-4.2.git] / gcc / llvm-convert.cpp
blob0cfa01b97f9787cc92121e7c807f076297f3c882
1 /* LLVM LOCAL begin (ENTIRE FILE!) */
2 /* High-level LLVM backend interface
3 Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4 Contributed by Chris Lattner (sabre@nondot.org)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 //===----------------------------------------------------------------------===//
24 // This is the code that converts GCC AST nodes into LLVM code.
25 //===----------------------------------------------------------------------===//
27 #include "llvm/ValueSymbolTable.h"
28 #include "llvm/CallingConv.h"
29 #include "llvm/Constants.h"
30 #include "llvm/DerivedTypes.h"
31 #include "llvm/InlineAsm.h"
32 #include "llvm/Instructions.h"
33 #include "llvm/LLVMContext.h"
34 #include "llvm/Module.h"
35 #include "llvm/Analysis/ConstantFolding.h"
36 #include "llvm/System/Host.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Target/TargetLowering.h"
40 #include "llvm/Target/TargetData.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/ADT/StringExtras.h"
43 #include "llvm/ADT/DenseMap.h"
45 #include "llvm-abi.h"
46 #include "llvm-internal.h"
47 #include "llvm-debug.h"
49 extern "C" {
50 #include "config.h"
51 #include "system.h"
52 #include "coretypes.h"
53 #include "tm.h"
54 #include "tm_p.h"
55 #include "tree.h"
56 #include "c-tree.h" // FIXME: eliminate.
57 #include "tree-iterator.h"
58 #include "output.h"
59 #include "diagnostic.h"
60 #include "real.h"
61 #include "langhooks.h"
62 #include "function.h"
63 #include "toplev.h"
64 #include "flags.h"
65 #include "target.h"
66 #include "hard-reg-set.h"
67 #include "except.h"
68 #include "rtl.h"
69 #include "libfuncs.h"
70 #include "tree-flow.h"
71 #include "tree-gimple.h"
72 extern int get_pointer_alignment (tree exp, unsigned int max_align);
73 extern enum machine_mode reg_raw_mode[FIRST_PSEUDO_REGISTER];
76 static LLVMContext &Context = getGlobalContext();
78 // Check for GCC bug 17347: C++ FE sometimes creates bogus ctor trees
79 // which we should throw out
80 #define BOGUS_CTOR(exp) \
81 (DECL_INITIAL(exp) && \
82 TREE_CODE(DECL_INITIAL(exp)) == CONSTRUCTOR && \
83 !TREE_TYPE(DECL_INITIAL(exp)))
85 /// isGimpleTemporary - Return true if this is a gimple temporary that we can
86 /// directly compile into an LLVM temporary. This saves us from creating an
87 /// alloca and creating loads/stores of that alloca (a compile-time win). We
88 /// can only do this if the value is a first class llvm value and if it's a
89 /// "gimple_formal_tmp_reg".
90 static bool isGimpleTemporary(tree decl) {
91 return is_gimple_formal_tmp_reg(decl) &&
92 !isAggregateTreeType(TREE_TYPE(decl));
95 /// getINTEGER_CSTVal - Return the specified INTEGER_CST value as a uint64_t.
96 ///
97 uint64_t getINTEGER_CSTVal(tree exp) {
98 unsigned HOST_WIDE_INT HI = (unsigned HOST_WIDE_INT)TREE_INT_CST_HIGH(exp);
99 unsigned HOST_WIDE_INT LO = (unsigned HOST_WIDE_INT)TREE_INT_CST_LOW(exp);
100 if (HOST_BITS_PER_WIDE_INT == 64) {
101 return (uint64_t)LO;
102 } else {
103 assert(HOST_BITS_PER_WIDE_INT == 32 &&
104 "Only 32- and 64-bit hosts supported!");
105 return ((uint64_t)HI << 32) | (uint64_t)LO;
109 /// isInt64 - Return true if t is an INTEGER_CST that fits in a 64 bit integer.
110 /// If Unsigned is false, returns whether it fits in a int64_t. If Unsigned is
111 /// true, returns whether the value is non-negative and fits in a uint64_t.
112 /// Always returns false for overflowed constants.
113 bool isInt64(tree t, bool Unsigned) {
114 if (HOST_BITS_PER_WIDE_INT == 64)
115 return host_integerp(t, Unsigned) && !TREE_OVERFLOW (t);
116 else {
117 assert(HOST_BITS_PER_WIDE_INT == 32 &&
118 "Only 32- and 64-bit hosts supported!");
119 return
120 (TREE_CODE (t) == INTEGER_CST && !TREE_OVERFLOW (t))
121 && ((TYPE_UNSIGNED(TREE_TYPE(t)) == Unsigned) ||
122 // If the constant is signed and we want an unsigned result, check
123 // that the value is non-negative. If the constant is unsigned and
124 // we want a signed result, check it fits in 63 bits.
125 (HOST_WIDE_INT)TREE_INT_CST_HIGH(t) >= 0);
129 /// getInt64 - Extract the value of an INTEGER_CST as a 64 bit integer. If
130 /// Unsigned is false, the value must fit in a int64_t. If Unsigned is true,
131 /// the value must be non-negative and fit in a uint64_t. Must not be used on
132 /// overflowed constants. These conditions can be checked by calling isInt64.
133 uint64_t getInt64(tree t, bool Unsigned) {
134 assert(isInt64(t, Unsigned) && "invalid constant!");
135 return getINTEGER_CSTVal(t);
138 /// getPointerAlignment - Return the alignment in bytes of exp, a pointer valued
139 /// expression, or 1 if the alignment is not known.
140 static unsigned int getPointerAlignment(tree exp) {
141 assert(POINTER_TYPE_P (TREE_TYPE (exp)) && "Expected a pointer type!");
142 unsigned int align = get_pointer_alignment(exp, BIGGEST_ALIGNMENT) / 8;
143 return align ? align : 1;
146 //===----------------------------------------------------------------------===//
147 // ... High-Level Methods ...
148 //===----------------------------------------------------------------------===//
150 /// TheTreeToLLVM - Keep track of the current function being compiled.
151 static TreeToLLVM *TheTreeToLLVM = 0;
153 const TargetData &getTargetData() {
154 return *TheTarget->getTargetData();
157 TreeToLLVM::TreeToLLVM(tree fndecl) :
158 TD(getTargetData()), Builder(Context, *TheFolder) {
159 FnDecl = fndecl;
160 Fn = 0;
161 ReturnBB = UnwindBB = 0;
162 ReturnOffset = 0;
164 if (TheDebugInfo) {
165 expanded_location Location = expand_location(DECL_SOURCE_LOCATION (fndecl));
167 if (Location.file) {
168 TheDebugInfo->setLocationFile(Location.file);
169 TheDebugInfo->setLocationLine(Location.line);
170 } else {
171 TheDebugInfo->setLocationFile("<unknown file>");
172 TheDebugInfo->setLocationLine(0);
176 AllocaInsertionPoint = 0;
178 ExceptionValue = 0;
179 ExceptionSelectorValue = 0;
180 FuncEHException = 0;
181 FuncEHSelector = 0;
182 FuncEHGetTypeID = 0;
184 assert(TheTreeToLLVM == 0 && "Reentering function creation?");
185 TheTreeToLLVM = this;
188 TreeToLLVM::~TreeToLLVM() {
189 TheTreeToLLVM = 0;
192 /// getLabelDeclBlock - Lazily get and create a basic block for the specified
193 /// label.
194 static BasicBlock *getLabelDeclBlock(tree LabelDecl) {
195 assert(TREE_CODE(LabelDecl) == LABEL_DECL && "Isn't a label!?");
196 if (DECL_LLVM_SET_P(LabelDecl))
197 return cast<BasicBlock>(DECL_LLVM(LabelDecl));
199 const char *Name = "bb";
200 if (DECL_NAME(LabelDecl))
201 Name = IDENTIFIER_POINTER(DECL_NAME(LabelDecl));
203 BasicBlock *NewBB = BasicBlock::Create(Context, Name);
204 SET_DECL_LLVM(LabelDecl, NewBB);
205 return NewBB;
208 /// llvm_store_scalar_argument - Store scalar argument ARGVAL of type
209 /// LLVMTY at location LOC.
210 static void llvm_store_scalar_argument(Value *Loc, Value *ArgVal,
211 const llvm::Type *LLVMTy,
212 unsigned RealSize,
213 LLVMBuilder &Builder) {
214 if (RealSize) {
215 // Not clear what this is supposed to do on big endian machines...
216 assert(!BYTES_BIG_ENDIAN && "Unsupported case - please report");
217 // Do byte wise store because actual argument type does not match LLVMTy.
218 assert(isa<IntegerType>(ArgVal->getType()) && "Expected an integer value!");
219 const Type *StoreType = IntegerType::get(Context, RealSize * 8);
220 Loc = Builder.CreateBitCast(Loc, StoreType->getPointerTo());
221 if (ArgVal->getType()->getPrimitiveSizeInBits() >=
222 StoreType->getPrimitiveSizeInBits())
223 ArgVal = Builder.CreateTrunc(ArgVal, StoreType);
224 else
225 ArgVal = Builder.CreateZExt(ArgVal, StoreType);
226 Builder.CreateStore(ArgVal, Loc);
227 } else {
228 // This cast only involves pointers, therefore BitCast.
229 Loc = Builder.CreateBitCast(Loc, LLVMTy->getPointerTo());
230 Builder.CreateStore(ArgVal, Loc);
234 #ifndef LLVM_STORE_SCALAR_ARGUMENT
235 #define LLVM_STORE_SCALAR_ARGUMENT(LOC,ARG,TYPE,SIZE,BUILDER) \
236 llvm_store_scalar_argument((LOC),(ARG),(TYPE),(SIZE),(BUILDER))
237 #endif
239 namespace {
240 /// FunctionPrologArgumentConversion - This helper class is driven by the ABI
241 /// definition for this target to figure out how to retrieve arguments from
242 /// the stack/regs coming into a function and store them into an appropriate
243 /// alloca for the argument.
244 struct FunctionPrologArgumentConversion : public DefaultABIClient {
245 tree FunctionDecl;
246 Function::arg_iterator &AI;
247 LLVMBuilder Builder;
248 std::vector<Value*> LocStack;
249 std::vector<std::string> NameStack;
250 unsigned Offset;
251 CallingConv::ID &CallingConv;
252 bool isShadowRet;
253 FunctionPrologArgumentConversion(tree FnDecl,
254 Function::arg_iterator &ai,
255 const LLVMBuilder &B, CallingConv::ID &CC)
256 : FunctionDecl(FnDecl), AI(ai), Builder(B), Offset(0), CallingConv(CC),
257 isShadowRet(false) {}
259 /// getCallingConv - This provides the desired CallingConv for the function.
260 CallingConv::ID& getCallingConv(void) { return CallingConv; }
262 bool isShadowReturn() const {
263 return isShadowRet;
265 void setName(const std::string &Name) {
266 NameStack.push_back(Name);
268 void setLocation(Value *Loc) {
269 LocStack.push_back(Loc);
271 void clear() {
272 assert(NameStack.size() == 1 && LocStack.size() == 1 && "Imbalance!");
273 NameStack.clear();
274 LocStack.clear();
277 void HandleAggregateShadowResult(const PointerType *PtrArgTy,
278 bool RetPtr) {
279 // If the function returns a structure by value, we transform the function
280 // to take a pointer to the result as the first argument of the function
281 // instead.
282 assert(AI != Builder.GetInsertBlock()->getParent()->arg_end() &&
283 "No explicit return value?");
284 AI->setName("agg.result");
286 isShadowRet = true;
287 tree ResultDecl = DECL_RESULT(FunctionDecl);
288 tree RetTy = TREE_TYPE(TREE_TYPE(FunctionDecl));
289 if (TREE_CODE(RetTy) == TREE_CODE(TREE_TYPE(ResultDecl))) {
290 SET_DECL_LLVM(ResultDecl, AI);
291 ++AI;
292 return;
295 // Otherwise, this must be something returned with NRVO.
296 assert(TREE_CODE(TREE_TYPE(ResultDecl)) == REFERENCE_TYPE &&
297 "Not type match and not passing by reference?");
298 // Create an alloca for the ResultDecl.
299 Value *Tmp = TheTreeToLLVM->CreateTemporary(AI->getType());
300 Builder.CreateStore(AI, Tmp);
302 SET_DECL_LLVM(ResultDecl, Tmp);
303 if (TheDebugInfo) {
304 TheDebugInfo->EmitDeclare(ResultDecl,
305 dwarf::DW_TAG_return_variable,
306 "agg.result", RetTy, Tmp,
307 Builder);
309 ++AI;
312 void HandleScalarShadowResult(const PointerType *PtrArgTy, bool RetPtr) {
313 assert(AI != Builder.GetInsertBlock()->getParent()->arg_end() &&
314 "No explicit return value?");
315 AI->setName("scalar.result");
316 isShadowRet = true;
317 SET_DECL_LLVM(DECL_RESULT(FunctionDecl), AI);
318 ++AI;
321 void HandleScalarArgument(const llvm::Type *LLVMTy, tree type,
322 unsigned RealSize = 0) {
323 Value *ArgVal = AI;
324 if (ArgVal->getType() != LLVMTy) {
325 if (isa<PointerType>(ArgVal->getType()) && isa<PointerType>(LLVMTy)) {
326 // If this is GCC being sloppy about pointer types, insert a bitcast.
327 // See PR1083 for an example.
328 ArgVal = Builder.CreateBitCast(ArgVal, LLVMTy);
329 } else if (ArgVal->getType()->isDoubleTy()) {
330 // If this is a K&R float parameter, it got promoted to double. Insert
331 // the truncation to float now.
332 ArgVal = Builder.CreateFPTrunc(ArgVal, LLVMTy,
333 NameStack.back().c_str());
334 } else {
335 // If this is just a mismatch between integer types, this is due
336 // to K&R prototypes, where the forward proto defines the arg as int
337 // and the actual impls is a short or char.
338 assert(ArgVal->getType()->isInteger(32) && LLVMTy->isInteger() &&
339 "Lowerings don't match?");
340 ArgVal = Builder.CreateTrunc(ArgVal, LLVMTy,NameStack.back().c_str());
343 assert(!LocStack.empty());
344 Value *Loc = LocStack.back();
345 LLVM_STORE_SCALAR_ARGUMENT(Loc,ArgVal,LLVMTy,RealSize,Builder);
346 AI->setName(NameStack.back());
347 ++AI;
350 void HandleByValArgument(const llvm::Type *LLVMTy, tree type) {
351 // Should not get here.
352 abort();
355 void HandleFCAArgument(const llvm::Type *LLVMTy,
356 tree type ATTRIBUTE_UNUSED) {
357 // Store the FCA argument into alloca.
358 assert(!LocStack.empty());
359 Value *Loc = LocStack.back();
360 Builder.CreateStore(AI, Loc);
361 AI->setName(NameStack.back());
362 ++AI;
365 void HandleAggregateResultAsScalar(const Type *ScalarTy, unsigned Offset=0){
366 this->Offset = Offset;
369 void EnterField(unsigned FieldNo, const llvm::Type *StructTy) {
370 NameStack.push_back(NameStack.back()+"."+utostr(FieldNo));
372 Value *Loc = LocStack.back();
373 // This cast only involves pointers, therefore BitCast.
374 Loc = Builder.CreateBitCast(Loc, StructTy->getPointerTo());
376 Loc = Builder.CreateStructGEP(Loc, FieldNo);
377 LocStack.push_back(Loc);
379 void ExitField() {
380 NameStack.pop_back();
381 LocStack.pop_back();
386 // isPassedByVal - Return true if an aggregate of the specified type will be
387 // passed in memory byval.
388 static bool isPassedByVal(tree type, const Type *Ty,
389 std::vector<const Type*> &ScalarArgs,
390 bool isShadowRet, CallingConv::ID &CC) {
391 if (LLVM_SHOULD_PASS_AGGREGATE_USING_BYVAL_ATTR(type, Ty))
392 return true;
394 std::vector<const Type*> Args;
395 if (LLVM_SHOULD_PASS_AGGREGATE_IN_MIXED_REGS(type, Ty, CC, Args) &&
396 LLVM_AGGREGATE_PARTIALLY_PASSED_IN_REGS(Args, ScalarArgs, isShadowRet,
397 CC))
398 // We want to pass the whole aggregate in registers but only some of the
399 // registers are available.
400 return true;
401 return false;
404 /// LanguageIsC - Return true if we are compiling C or Objective-C.
405 static bool LanguageIsC() {
406 // If we've already determined this, return it.
407 static unsigned Val = 2;
408 if (Val != 2) return (bool)Val;
410 StringRef LanguageName = lang_hooks.name;
412 if (LanguageName == "GNU C" || LanguageName == "GNU Objective-C")
413 return (Val = true);
414 return (Val = false);
417 // Walk the GCC BLOCK() tree. Set BLOCK_NUMBER() to the depth of each
418 // block; this is necessary for lexical block debug info. Visit all
419 // the BLOCK_VARS(), and add them to the set s. Since the
420 // unexpanded_var_list seems to be a superset of all the scoped
421 // variables in every lexical BLOCK(), this facilitates allocating the
422 // scoped variables in their blocks, and the rest at the outermost
423 // scope of the function.
424 void TreeToLLVM::setLexicalBlockDepths(tree t, treeset &s, unsigned level) {
425 tree bstep, step;
426 switch (TREE_CODE(t)) {
427 default:
428 abort();
429 case BLOCK:
430 for (bstep = t; bstep; bstep = TREE_CHAIN(bstep)) {
431 BLOCK_NUMBER(bstep) = level;
432 for (step = BLOCK_VARS(t); step; step = TREE_CHAIN(step))
433 s.insert(step);
435 for (bstep = BLOCK_SUBBLOCKS(t); bstep; bstep = TREE_CHAIN(bstep))
436 setLexicalBlockDepths(bstep, s, level+1);
437 return;
438 case FUNCTION_DECL:
439 return setLexicalBlockDepths(DECL_INITIAL(t), s, level);
443 void TreeToLLVM::StartFunctionBody() {
444 const char *Name = "";
445 // Get the name of the function.
446 if (tree ID = DECL_ASSEMBLER_NAME(FnDecl))
447 Name = IDENTIFIER_POINTER(ID);
449 // Determine the FunctionType and calling convention for this function.
450 tree static_chain = cfun->static_chain_decl;
451 const FunctionType *FTy;
452 CallingConv::ID CallingConv;
453 AttrListPtr PAL;
455 // If the function has no arguments and is varargs (...), turn it into a
456 // non-varargs function by scanning the param list for the function. This
457 // allows C functions declared as "T foo() {}" to be treated like
458 // "T foo(void) {}" and allows us to handle functions with K&R-style
459 // definitions correctly.
461 // Note that we only do this in C/Objective-C. Doing this in C++ for
462 // functions explicitly declared as taking (...) is bad.
463 if (TYPE_ARG_TYPES(TREE_TYPE(FnDecl)) == 0 && LanguageIsC()) {
464 FTy = TheTypeConverter->ConvertArgListToFnType(TREE_TYPE(FnDecl),
465 DECL_ARGUMENTS(FnDecl),
466 static_chain,
467 CallingConv, PAL);
468 } else {
469 // Otherwise, just get the type from the function itself.
470 FTy = TheTypeConverter->ConvertFunctionType(TREE_TYPE(FnDecl),
471 FnDecl,
472 static_chain,
473 CallingConv, PAL);
476 // If we've already seen this function and created a prototype, and if the
477 // proto has the right LLVM type, just use it.
478 if (DECL_LLVM_SET_P(FnDecl) &&
479 cast<PointerType>(DECL_LLVM(FnDecl)->getType())->getElementType() == FTy){
480 Fn = cast<Function>(DECL_LLVM(FnDecl));
481 assert(Fn->getCallingConv() == CallingConv &&
482 "Calling convention disagreement between prototype and impl!");
483 // The visibility can be changed from the last time we've seen this
484 // function. Set to current.
485 handleVisibility(FnDecl, Fn);
486 } else {
487 Function *FnEntry = TheModule->getFunction(Name);
488 if (FnEntry) {
489 assert(FnEntry->getName() == Name && "Same entry, different name?");
490 assert((FnEntry->isDeclaration() ||
491 FnEntry->getLinkage() == Function::AvailableExternallyLinkage) &&
492 "Multiple fns with same name and neither are external!");
493 FnEntry->setName(""); // Clear name to avoid conflicts.
494 assert(FnEntry->getCallingConv() == CallingConv &&
495 "Calling convention disagreement between prototype and impl!");
498 // Otherwise, either it exists with the wrong type or it doesn't exist. In
499 // either case create a new function.
500 Fn = Function::Create(FTy, Function::ExternalLinkage, Name, TheModule);
501 assert(Fn->getName() == Name && "Preexisting fn with the same name!");
502 Fn->setCallingConv(CallingConv);
503 Fn->setAttributes(PAL);
505 // If a previous proto existed with the wrong type, replace any uses of it
506 // with the actual function and delete the proto.
507 if (FnEntry) {
508 FnEntry->replaceAllUsesWith(
509 Builder.getFolder().CreateBitCast(Fn, FnEntry->getType())
511 changeLLVMConstant(FnEntry, Fn);
512 FnEntry->eraseFromParent();
514 SET_DECL_LLVM(FnDecl, Fn);
517 // The function should not already have a body.
518 assert(Fn->empty() && "Function expanded multiple times!");
520 // Compute the linkage that the function should get.
521 // Functions declared "always inline" should not have a body
522 // emitted; hack this by pretending they're static. That will either
523 // make them go away or emit a static definition that won't collide with
524 // anything.
525 if (DECL_LLVM_PRIVATE(FnDecl)) {
526 Fn->setLinkage(Function::PrivateLinkage);
527 } else if (DECL_LLVM_LINKER_PRIVATE(FnDecl)) {
528 Fn->setLinkage(Function::LinkerPrivateLinkage);
529 } else if (!TREE_PUBLIC(FnDecl) /*|| lang_hooks.llvm_is_in_anon(subr)*/) {
530 Fn->setLinkage(Function::InternalLinkage);
531 } else if (DECL_EXTERNAL(FnDecl) &&
532 lookup_attribute ("always_inline", DECL_ATTRIBUTES (FnDecl))) {
533 Fn->setLinkage(Function::InternalLinkage);
534 } else if (DECL_COMDAT(FnDecl)) {
535 Fn->setLinkage(Function::getLinkOnceLinkage(flag_odr));
536 } else if (DECL_WEAK(FnDecl)) {
537 // The user may have explicitly asked for weak linkage - ignore flag_odr.
538 Fn->setLinkage(Function::WeakAnyLinkage);
539 } else if (DECL_ONE_ONLY(FnDecl)) {
540 Fn->setLinkage(Function::getWeakLinkage(flag_odr));
543 #ifdef TARGET_ADJUST_LLVM_LINKAGE
544 TARGET_ADJUST_LLVM_LINKAGE(Fn,FnDecl);
545 #endif /* TARGET_ADJUST_LLVM_LINKAGE */
547 // Handle visibility style
548 handleVisibility(FnDecl, Fn);
550 // Handle attribute "aligned".
551 if (DECL_ALIGN (FnDecl) != FUNCTION_BOUNDARY)
552 Fn->setAlignment(DECL_ALIGN (FnDecl) / 8);
554 // Handle functions in specified sections.
555 if (DECL_SECTION_NAME(FnDecl))
556 Fn->setSection(TREE_STRING_POINTER(DECL_SECTION_NAME(FnDecl)));
558 // Handle used Functions
559 if (lookup_attribute ("used", DECL_ATTRIBUTES (FnDecl)))
560 AttributeUsedGlobals.insert(Fn);
562 // Handle noinline Functions
563 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (FnDecl)))
564 Fn->addFnAttr(Attribute::NoInline);
566 // Handle always_inline attribute
567 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (FnDecl)))
568 Fn->addFnAttr(Attribute::AlwaysInline);
570 if (optimize_size)
571 Fn->addFnAttr(Attribute::OptimizeForSize);
573 // Handle stack smashing protection.
574 if (flag_stack_protect == 1)
575 Fn->addFnAttr(Attribute::StackProtect);
576 else if (flag_stack_protect == 2)
577 Fn->addFnAttr(Attribute::StackProtectReq);
579 // Handle naked attribute
580 if (lookup_attribute ("naked", DECL_ATTRIBUTES (FnDecl)))
581 Fn->addFnAttr(Attribute::Naked);
583 // Handle annotate attributes
584 if (DECL_ATTRIBUTES(FnDecl))
585 AddAnnotateAttrsToGlobal(Fn, FnDecl);
587 // Mark the function "nounwind" if not doing exception handling.
588 if (!flag_exceptions)
589 Fn->setDoesNotThrow();
591 // Create a new basic block for the function.
592 Builder.SetInsertPoint(BasicBlock::Create(Context, "entry", Fn));
594 treeset block_declared_vars;
595 // Set the BLOCK_NUMBER()s to the depth of each lexical block.
596 setLexicalBlockDepths(FnDecl, block_declared_vars, 1);
598 SeenBlocks.clear();
600 if (TheDebugInfo)
601 TheDebugInfo->EmitFunctionStart(FnDecl, Fn, Builder.GetInsertBlock());
603 // Loop over all of the arguments to the function, setting Argument names and
604 // creating argument alloca's for the PARM_DECLs in case their address is
605 // exposed.
606 Function::arg_iterator AI = Fn->arg_begin();
608 // Rename and alloca'ify real arguments.
609 FunctionPrologArgumentConversion Client(FnDecl, AI, Builder, CallingConv);
610 TheLLVMABI ABIConverter(Client);
612 // Handle the DECL_RESULT.
613 ABIConverter.HandleReturnType(TREE_TYPE(TREE_TYPE(FnDecl)), FnDecl,
614 DECL_BUILT_IN(FnDecl));
615 // Remember this for use by FinishFunctionBody.
616 TheTreeToLLVM->ReturnOffset = Client.Offset;
618 // Prepend the static chain (if any) to the list of arguments.
619 tree Args = static_chain ? static_chain : DECL_ARGUMENTS(FnDecl);
621 // Scalar arguments processed so far.
622 std::vector<const Type*> ScalarArgs;
623 while (Args) {
624 const char *Name = "unnamed_arg";
625 if (DECL_NAME(Args)) Name = IDENTIFIER_POINTER(DECL_NAME(Args));
627 const Type *ArgTy = ConvertType(TREE_TYPE(Args));
628 bool isInvRef = isPassedByInvisibleReference(TREE_TYPE(Args));
629 if (isInvRef ||
630 (isa<VectorType>(ArgTy) &&
631 LLVM_SHOULD_PASS_VECTOR_USING_BYVAL_ATTR(TREE_TYPE(Args))) ||
632 (!ArgTy->isSingleValueType() &&
633 isPassedByVal(TREE_TYPE(Args), ArgTy, ScalarArgs,
634 Client.isShadowReturn(), CallingConv))) {
635 // If the value is passed by 'invisible reference' or 'byval reference',
636 // the l-value for the argument IS the argument itself.
637 AI->setName(Name);
638 SET_DECL_LLVM(Args, AI);
639 if (!isInvRef && TheDebugInfo)
640 TheDebugInfo->EmitDeclare(Args, dwarf::DW_TAG_arg_variable,
641 Name, TREE_TYPE(Args),
642 AI, Builder);
643 ++AI;
644 } else {
645 // Otherwise, we create an alloca to hold the argument value and provide
646 // an l-value. On entry to the function, we copy formal argument values
647 // into the alloca.
648 Value *Tmp = CreateTemporary(ArgTy);
649 Tmp->setName(std::string(Name)+"_addr");
650 SET_DECL_LLVM(Args, Tmp);
651 if (TheDebugInfo) {
652 TheDebugInfo->EmitDeclare(Args, dwarf::DW_TAG_arg_variable,
653 Name, TREE_TYPE(Args), Tmp,
654 Builder);
657 // Emit annotate intrinsic if arg has annotate attr
658 if (DECL_ATTRIBUTES(Args))
659 EmitAnnotateIntrinsic(Tmp, Args);
661 // Emit gcroot intrinsic if arg has attribute
662 if (POINTER_TYPE_P(TREE_TYPE(Args))
663 && lookup_attribute ("gcroot", TYPE_ATTRIBUTES(TREE_TYPE(Args))))
664 EmitTypeGcroot(Tmp, Args);
666 Client.setName(Name);
667 Client.setLocation(Tmp);
668 ABIConverter.HandleArgument(TREE_TYPE(Args), ScalarArgs);
669 Client.clear();
672 Args = Args == static_chain ? DECL_ARGUMENTS(FnDecl) : TREE_CHAIN(Args);
675 // If this is not a void-returning function, initialize the RESULT_DECL.
676 if (DECL_RESULT(FnDecl) && !VOID_TYPE_P(TREE_TYPE(DECL_RESULT(FnDecl))) &&
677 !DECL_LLVM_SET_P(DECL_RESULT(FnDecl)))
678 EmitAutomaticVariableDecl(DECL_RESULT(FnDecl));
680 // If this function has nested functions, we should handle a potential
681 // nonlocal_goto_save_area.
682 if (cfun->nonlocal_goto_save_area) {
683 // Not supported yet.
686 if (TheDebugInfo)
687 TheDebugInfo->EmitStopPoint(Fn, Builder.GetInsertBlock(), Builder);
689 // As it turns out, not all temporaries are associated with blocks. For those
690 // that aren't, emit them now.
691 for (tree t = cfun->unexpanded_var_list; t; t = TREE_CHAIN(t)) {
692 // If this variable hasn't been emitted into LLVM yet, AND it is
693 // not part of any BLOCK, emit it now.
694 if (!DECL_LLVM_SET_P(TREE_VALUE(t)) &&
695 block_declared_vars.count(TREE_VALUE(t)) == 0)
696 EmitAutomaticVariableDecl(TREE_VALUE(t));
699 // Push the outermost lexical block onto the RegionStack.
700 switchLexicalBlock(DECL_INITIAL(FnDecl));
702 // Create a new block for the return node, but don't insert it yet.
703 ReturnBB = BasicBlock::Create(Context, "return");
706 Function *TreeToLLVM::FinishFunctionBody() {
707 // Insert the return block at the end of the function.
708 EmitBlock(ReturnBB);
710 SmallVector <Value *, 4> RetVals;
712 // If the function returns a value, get it into a register and return it now.
713 if (Fn->getReturnType() != Type::getVoidTy(Context)) {
714 if (!isAggregateTreeType(TREE_TYPE(DECL_RESULT(FnDecl)))) {
715 // If the DECL_RESULT is a scalar type, just load out the return value
716 // and return it.
717 tree TreeRetVal = DECL_RESULT(FnDecl);
718 Value *RetVal = Builder.CreateLoad(DECL_LLVM(TreeRetVal), "retval");
719 bool RetValSigned = !TYPE_UNSIGNED(TREE_TYPE(TreeRetVal));
720 Instruction::CastOps opcode = CastInst::getCastOpcode(
721 RetVal, RetValSigned, Fn->getReturnType(), RetValSigned);
722 RetVal = CastToType(opcode, RetVal, Fn->getReturnType());
723 RetVals.push_back(RetVal);
724 } else {
725 Value *RetVal = DECL_LLVM(DECL_RESULT(FnDecl));
726 if (const StructType *STy = dyn_cast<StructType>(Fn->getReturnType())) {
727 Value *R1 = BitCastToType(RetVal, STy->getPointerTo());
729 llvm::Value *Idxs[2];
730 Idxs[0] = ConstantInt::get(llvm::Type::getInt32Ty(Context), 0);
731 for (unsigned ri = 0; ri < STy->getNumElements(); ++ri) {
732 Idxs[1] = ConstantInt::get(llvm::Type::getInt32Ty(Context), ri);
733 Value *GEP = Builder.CreateGEP(R1, Idxs, Idxs+2, "mrv_gep");
734 Value *E = Builder.CreateLoad(GEP, "mrv");
735 RetVals.push_back(E);
737 } else {
738 // Otherwise, this aggregate result must be something that is returned
739 // in a scalar register for this target. We must bit convert the
740 // aggregate to the specified scalar type, which we do by casting the
741 // pointer and loading. The load does not necessarily start at the
742 // beginning of the aggregate (x86-64).
743 if (ReturnOffset) {
744 RetVal = BitCastToType(RetVal,
745 Type::getInt8PtrTy(Context));
746 RetVal = Builder.CreateGEP(RetVal,
747 ConstantInt::get(TD.getIntPtrType(Context), ReturnOffset));
749 RetVal = BitCastToType(RetVal, Fn->getReturnType()->getPointerTo());
750 RetVal = Builder.CreateLoad(RetVal, "retval");
751 RetVals.push_back(RetVal);
755 if (TheDebugInfo) {
756 TheDebugInfo->EmitStopPoint(Fn, Builder.GetInsertBlock(), Builder);
757 TheDebugInfo->EmitFunctionEnd(Builder.GetInsertBlock(), true);
759 if (RetVals.empty())
760 Builder.CreateRetVoid();
761 else if (!Fn->getReturnType()->isAggregateType()) {
762 assert(RetVals.size() == 1 && "Non-aggregate return has multiple values!");
763 Builder.CreateRet(RetVals[0]);
764 } else
765 Builder.CreateAggregateRet(RetVals.data(), RetVals.size());
767 // Emit pending exception handling code.
768 EmitLandingPads();
769 EmitPostPads();
770 EmitUnwindBlock();
772 // Remove any cached LLVM values that are local to this function. Such values
773 // may be deleted when the optimizers run, so would be dangerous to keep.
774 eraseLocalLLVMValues();
776 // Simplify any values that were uniqued using a no-op bitcast.
777 for (std::vector<BitCastInst *>::iterator I = UniquedValues.begin(),
778 E = UniquedValues.end(); I != E; ++I) {
779 BitCastInst *BI = *I;
780 assert(BI->getSrcTy() == BI->getDestTy() && "Not a no-op bitcast!");
781 BI->replaceAllUsesWith(BI->getOperand(0));
782 // Safe to erase because after the call to eraseLocalLLVMValues.
783 BI->eraseFromParent();
785 UniquedValues.clear();
787 return Fn;
790 Function *TreeToLLVM::EmitFunction() {
791 // Set up parameters and prepare for return, for the function.
792 StartFunctionBody();
794 // Emit the body of the function iterating over all BBs
795 basic_block bb;
796 edge e;
797 edge_iterator ei;
798 FOR_EACH_BB (bb) {
799 for (block_stmt_iterator bsi = bsi_start (bb); !bsi_end_p (bsi);
800 bsi_next (&bsi)) {
801 MemRef DestLoc;
802 tree stmt = bsi_stmt (bsi);
804 // If this stmt returns an aggregate value (e.g. a call whose result is
805 // ignored), create a temporary to receive the value. Note that we don't
806 // do this for MODIFY_EXPRs as an efficiency hack.
807 if (isAggregateTreeType(TREE_TYPE(stmt)) &&
808 TREE_CODE(stmt)!= MODIFY_EXPR && TREE_CODE(stmt)!=INIT_EXPR)
809 DestLoc = CreateTempLoc(ConvertType(TREE_TYPE(stmt)));
811 Emit(stmt, DestLoc.Ptr ? &DestLoc : NULL);
814 FOR_EACH_EDGE (e, ei, bb->succs)
815 if (e->flags & EDGE_FALLTHRU)
816 break;
817 if (e && e->dest != bb->next_bb) {
818 Builder.CreateBr(getLabelDeclBlock(tree_block_label (e->dest)));
819 EmitBlock(BasicBlock::Create(Context, ""));
823 // Wrap things up.
824 return FinishFunctionBody();
827 // Given a GCC lexical context (BLOCK or FUNCTION_DECL), make it the
828 // new current BLOCK/context/scope. Emit any local variables found
829 // in the new context. Note that the variable emission order must be
830 // consistent with and without debug info; otherwise, the register
831 // allocation would change with -g, and users dislike that.
832 void TreeToLLVM::switchLexicalBlock(tree exp) {
833 if (exp == NULL_TREE || TREE_CODE(exp) == FUNCTION_DECL) {
834 // assert(RegionStack empty);
835 if (TheDebugInfo)
836 TheDebugInfo->setCurrentLexicalBlock(exp);
837 return;
840 // Only EXPR_P nodes have a TREE_BLOCK() field.
841 if (!EXPR_P(exp) && (TREE_CODE(exp) != BLOCK))
842 return;
844 tree new_block = EXPR_P(exp) ? TREE_BLOCK(exp) : exp;
846 if (!new_block)
847 return;
849 // Have we seen this BLOCK before?
850 bool previously_visited = !SeenBlocks.insert(new_block);
852 // If new_block is nested inside another block, and we haven't
853 // processed either block, insure the outer block(s) get processed
854 // first.
855 if (!previously_visited)
856 switchLexicalBlock(BLOCK_SUPERCONTEXT(new_block));
858 if (TheDebugInfo) {
859 tree current_block = TheDebugInfo->getCurrentLexicalBlock();
860 if (new_block && current_block && new_block != current_block) {
861 tree new_climber = new_block, current_climber = current_block;
862 unsigned new_climber_depth, current_climber_depth;
863 // Climb the GCC BLOCK hierarchy, looking for a common parent
864 // BLOCK or FUNCTION_DECL. Assume BLOCK_NUMBER() fields set to
865 // depth by setLexicalBlockDepths(). If the climber depths are
866 // equal, but the climbers differ, they are siblings: both
867 // climbers rise to the next level.
868 while (new_climber != current_climber) {
869 current_climber_depth = DECL_P(current_climber) ? 0 : BLOCK_NUMBER(current_climber);
870 new_climber_depth = DECL_P(new_climber) ? 0 : BLOCK_NUMBER(new_climber);
871 if (new_climber_depth <= current_climber_depth)
872 current_climber = BLOCK_SUPERCONTEXT(current_climber);
873 if (new_climber_depth >= current_climber_depth)
874 new_climber = BLOCK_SUPERCONTEXT(new_climber);
876 assert(new_climber == current_climber && "missed common TREE_BLOCK parent");
877 // Pop and push lexical blocks to arrive at the new context.
878 TheDebugInfo->change_regions(new_block, new_climber);
882 // If we've seen this lexical BLOCK before, we're done.
883 if (previously_visited)
884 return;
886 // Finally, allocate any BLOCK_VARS we find.
887 tree step;
888 for (step = BLOCK_VARS(new_block); step; step = TREE_CHAIN(step))
889 switch (TREE_CODE_CLASS(TREE_CODE(step))) {
890 default:
891 assert(0 && "non-var, non-type node hanging from a GCC BLOCK?");
892 break;
893 case tcc_type:
894 // GCC has recorded a lexical block scope for this type, but
895 // ConvertType() doesn't currently handle scopes for types.
896 break;
897 case tcc_declaration:
898 if (!DECL_LLVM_SET_P(step))
899 EmitAutomaticVariableDecl(step);
900 break;
903 // Kludge! The Objective-C++ front-end doesn't always point to the
904 // right BLOCK. :-( Test case g++.apple/block-assigncglobal.C has a
905 // block (closure) where the first statement (a MODIFY_EXPR) points
906 // to a lexical BLOCK that has no BLOCK_VARS, but it has a subblock
907 // with the decl for the target of the MODIFY. Ergo, if we
908 // encounter a BLOCK that allocates no variables, check for a
909 // subblock. A better fix would be in the front-end.
910 if (!BLOCK_VARS(new_block) && BLOCK_SUBBLOCKS(new_block))
911 switchLexicalBlock(BLOCK_SUBBLOCKS(new_block));
914 Value *TreeToLLVM::Emit(tree exp, const MemRef *DestLoc) {
915 assert((isAggregateTreeType(TREE_TYPE(exp)) == (DestLoc != 0) ||
916 TREE_CODE(exp) == MODIFY_EXPR || TREE_CODE(exp) == INIT_EXPR) &&
917 "Didn't pass DestLoc to an aggregate expr, or passed it to scalar!");
919 Value *Result = 0;
921 // If we've just changed lexical blocks, emit any local variables
922 // declared in the new block.
923 TreeToLLVM::switchLexicalBlock(exp);
925 if (TheDebugInfo) {
926 if (EXPR_HAS_LOCATION(exp)) {
927 // Set new location on the way up the tree.
928 TheDebugInfo->setLocationFile(EXPR_FILENAME(exp));
929 TheDebugInfo->setLocationLine(EXPR_LINENO(exp));
932 TheDebugInfo->EmitStopPoint(Fn, Builder.GetInsertBlock(), Builder);
935 switch (TREE_CODE(exp)) {
936 default:
937 errs() << "Unhandled expression!\n"
938 << "TREE_CODE: " << TREE_CODE(exp) << "\n";
939 debug_tree(exp);
940 abort();
942 // Control flow
943 case LABEL_EXPR: Result = EmitLABEL_EXPR(exp); break;
944 case GOTO_EXPR: Result = EmitGOTO_EXPR(exp); break;
945 case RETURN_EXPR: Result = EmitRETURN_EXPR(exp, DestLoc); break;
946 case COND_EXPR: Result = EmitCOND_EXPR(exp); break;
947 case SWITCH_EXPR: Result = EmitSWITCH_EXPR(exp); break;
949 // Exception handling.
950 case EXC_PTR_EXPR: Result = EmitEXC_PTR_EXPR(exp); break;
951 case FILTER_EXPR: Result = EmitFILTER_EXPR(exp); break;
952 case RESX_EXPR: Result = EmitRESX_EXPR(exp); break;
954 // Expressions
955 case VAR_DECL:
956 case PARM_DECL:
957 case RESULT_DECL:
958 case INDIRECT_REF:
959 case ARRAY_REF:
960 case ARRAY_RANGE_REF:
961 case COMPONENT_REF:
962 case BIT_FIELD_REF:
963 case STRING_CST:
964 case REALPART_EXPR:
965 case IMAGPART_EXPR:
966 Result = EmitLoadOfLValue(exp, DestLoc);
967 break;
968 case OBJ_TYPE_REF: Result = EmitOBJ_TYPE_REF(exp); break;
969 case ADDR_EXPR: Result = EmitADDR_EXPR(exp); break;
970 case CALL_EXPR: Result = EmitCALL_EXPR(exp, DestLoc); break;
971 case INIT_EXPR:
972 case MODIFY_EXPR: Result = EmitMODIFY_EXPR(exp, DestLoc); break;
973 case ASM_EXPR: Result = EmitASM_EXPR(exp); break;
974 case NON_LVALUE_EXPR: Result = Emit(TREE_OPERAND(exp, 0), DestLoc); break;
976 // Unary Operators
977 case NOP_EXPR: Result = EmitNOP_EXPR(exp, DestLoc); break;
978 case FIX_TRUNC_EXPR:
979 case FLOAT_EXPR:
980 case CONVERT_EXPR: Result = EmitCONVERT_EXPR(exp, DestLoc); break;
981 case VIEW_CONVERT_EXPR: Result = EmitVIEW_CONVERT_EXPR(exp, DestLoc); break;
982 case NEGATE_EXPR: Result = EmitNEGATE_EXPR(exp, DestLoc); break;
983 case CONJ_EXPR: Result = EmitCONJ_EXPR(exp, DestLoc); break;
984 case ABS_EXPR: Result = EmitABS_EXPR(exp); break;
985 case BIT_NOT_EXPR: Result = EmitBIT_NOT_EXPR(exp); break;
986 case TRUTH_NOT_EXPR: Result = EmitTRUTH_NOT_EXPR(exp); break;
988 // Binary Operators
989 case LT_EXPR:
990 Result = EmitCompare(exp, ICmpInst::ICMP_ULT, ICmpInst::ICMP_SLT,
991 FCmpInst::FCMP_OLT);
992 break;
993 case LE_EXPR:
994 Result = EmitCompare(exp, ICmpInst::ICMP_ULE, ICmpInst::ICMP_SLE,
995 FCmpInst::FCMP_OLE);
996 break;
997 case GT_EXPR:
998 Result = EmitCompare(exp, ICmpInst::ICMP_UGT, ICmpInst::ICMP_SGT,
999 FCmpInst::FCMP_OGT);
1000 break;
1001 case GE_EXPR:
1002 Result = EmitCompare(exp, ICmpInst::ICMP_UGE, ICmpInst::ICMP_SGE,
1003 FCmpInst::FCMP_OGE);
1004 break;
1005 case EQ_EXPR:
1006 Result = EmitCompare(exp, ICmpInst::ICMP_EQ, ICmpInst::ICMP_EQ,
1007 FCmpInst::FCMP_OEQ);
1008 break;
1009 case NE_EXPR:
1010 Result = EmitCompare(exp, ICmpInst::ICMP_NE, ICmpInst::ICMP_NE,
1011 FCmpInst::FCMP_UNE);
1012 break;
1013 case UNORDERED_EXPR:
1014 Result = EmitCompare(exp, 0, 0, FCmpInst::FCMP_UNO);
1015 break;
1016 case ORDERED_EXPR:
1017 Result = EmitCompare(exp, 0, 0, FCmpInst::FCMP_ORD);
1018 break;
1019 case UNLT_EXPR: Result = EmitCompare(exp, 0, 0, FCmpInst::FCMP_ULT); break;
1020 case UNLE_EXPR: Result = EmitCompare(exp, 0, 0, FCmpInst::FCMP_ULE); break;
1021 case UNGT_EXPR: Result = EmitCompare(exp, 0, 0, FCmpInst::FCMP_UGT); break;
1022 case UNGE_EXPR: Result = EmitCompare(exp, 0, 0, FCmpInst::FCMP_UGE); break;
1023 case UNEQ_EXPR: Result = EmitCompare(exp, 0, 0, FCmpInst::FCMP_UEQ); break;
1024 case LTGT_EXPR: Result = EmitCompare(exp, 0, 0, FCmpInst::FCMP_ONE); break;
1025 case PLUS_EXPR:
1026 Result = EmitBinOp(exp, DestLoc,
1027 FLOAT_TYPE_P(TREE_TYPE(exp)) ?
1028 Instruction::FAdd :
1029 Instruction::Add);
1030 break;
1031 case MINUS_EXPR:
1032 Result = EmitBinOp(exp, DestLoc,
1033 FLOAT_TYPE_P(TREE_TYPE(exp)) ?
1034 Instruction::FSub :
1035 Instruction::Sub);
1036 break;
1037 case MULT_EXPR:
1038 Result = EmitBinOp(exp, DestLoc,
1039 FLOAT_TYPE_P(TREE_TYPE(exp)) ?
1040 Instruction::FMul :
1041 Instruction::Mul);
1042 break;
1043 case EXACT_DIV_EXPR: Result = EmitEXACT_DIV_EXPR(exp, DestLoc); break;
1044 case TRUNC_DIV_EXPR:
1045 if (TYPE_UNSIGNED(TREE_TYPE(exp)))
1046 Result = EmitBinOp(exp, DestLoc, Instruction::UDiv);
1047 else
1048 Result = EmitBinOp(exp, DestLoc, Instruction::SDiv);
1049 break;
1050 case RDIV_EXPR: Result = EmitBinOp(exp, DestLoc, Instruction::FDiv); break;
1051 case CEIL_DIV_EXPR: Result = EmitCEIL_DIV_EXPR(exp); break;
1052 case FLOOR_DIV_EXPR: Result = EmitFLOOR_DIV_EXPR(exp); break;
1053 case ROUND_DIV_EXPR: Result = EmitROUND_DIV_EXPR(exp); break;
1054 case TRUNC_MOD_EXPR:
1055 if (TYPE_UNSIGNED(TREE_TYPE(exp)))
1056 Result = EmitBinOp(exp, DestLoc, Instruction::URem);
1057 else
1058 Result = EmitBinOp(exp, DestLoc, Instruction::SRem);
1059 break;
1060 case FLOOR_MOD_EXPR: Result = EmitFLOOR_MOD_EXPR(exp, DestLoc); break;
1061 case BIT_AND_EXPR: Result = EmitBinOp(exp, DestLoc, Instruction::And);break;
1062 case BIT_IOR_EXPR: Result = EmitBinOp(exp, DestLoc, Instruction::Or );break;
1063 case BIT_XOR_EXPR: Result = EmitBinOp(exp, DestLoc, Instruction::Xor);break;
1064 case TRUTH_AND_EXPR: Result = EmitTruthOp(exp, Instruction::And); break;
1065 case TRUTH_OR_EXPR: Result = EmitTruthOp(exp, Instruction::Or); break;
1066 case TRUTH_XOR_EXPR: Result = EmitTruthOp(exp, Instruction::Xor); break;
1067 case RSHIFT_EXPR:
1068 Result = EmitShiftOp(exp,DestLoc,
1069 TYPE_UNSIGNED(TREE_TYPE(exp)) ? Instruction::LShr : Instruction::AShr);
1070 break;
1071 case LSHIFT_EXPR: Result = EmitShiftOp(exp,DestLoc,Instruction::Shl);break;
1072 case RROTATE_EXPR:
1073 Result = EmitRotateOp(exp, Instruction::LShr, Instruction::Shl);
1074 break;
1075 case LROTATE_EXPR:
1076 Result = EmitRotateOp(exp, Instruction::Shl, Instruction::LShr);
1077 break;
1078 case MIN_EXPR:
1079 Result = EmitMinMaxExpr(exp, ICmpInst::ICMP_ULE, ICmpInst::ICMP_SLE,
1080 FCmpInst::FCMP_OLE);
1081 break;
1082 case MAX_EXPR:
1083 Result = EmitMinMaxExpr(exp, ICmpInst::ICMP_UGE, ICmpInst::ICMP_SGE,
1084 FCmpInst::FCMP_OGE);
1085 break;
1086 case CONSTRUCTOR: Result = EmitCONSTRUCTOR(exp, DestLoc); break;
1088 // Complex Math Expressions.
1089 case COMPLEX_CST: EmitCOMPLEX_CST (exp, DestLoc); break;
1090 case COMPLEX_EXPR: EmitCOMPLEX_EXPR(exp, DestLoc); break;
1092 // Constant Expressions
1093 case INTEGER_CST:
1094 Result = TreeConstantToLLVM::ConvertINTEGER_CST(exp);
1095 break;
1096 case REAL_CST:
1097 Result = TreeConstantToLLVM::ConvertREAL_CST(exp);
1098 break;
1099 case VECTOR_CST:
1100 Result = TreeConstantToLLVM::ConvertVECTOR_CST(exp);
1101 break;
1104 if (TheDebugInfo && EXPR_HAS_LOCATION(exp)) {
1105 // Restore location back down the tree.
1106 TheDebugInfo->setLocationFile(EXPR_FILENAME(exp));
1107 TheDebugInfo->setLocationLine(EXPR_LINENO(exp));
1110 assert(((DestLoc && Result == 0) || DestLoc == 0) &&
1111 "Expected a scalar or aggregate but got the wrong thing!");
1112 // Check that the type of the result matches that of the tree node. If the
1113 // result is not used then GCC sometimes sets the tree type to VOID_TYPE, so
1114 // don't take VOID_TYPE too seriously here.
1115 assert((Result == 0 || VOID_TYPE_P(TREE_TYPE(exp)) ||
1116 // FIXME: The vector stuff isn't straight-forward. Sometimes X86 can
1117 // pass it back as a scalar value. Disable checking if it's a
1118 // vector. This should be made better, though.
1119 isa<VectorType>(ConvertType(TREE_TYPE(exp))) ||
1120 // FIXME: The handling of MODIFY_EXPR doesn't always produce results
1121 // that pass this check; the return type might be the LHS type or
1122 // the RHS type, neither of which is guaranteed to be the
1123 // same as the expression type.
1124 TREE_CODE(exp) == MODIFY_EXPR ||
1125 Result->getType() == ConvertType(TREE_TYPE(exp))) &&
1126 "Value has wrong type!");
1127 return Result;
1130 /// get_constant_alignment - Return the alignment of constant EXP in bits.
1132 static unsigned int
1133 get_constant_alignment (tree exp)
1135 unsigned int align = TYPE_ALIGN (TREE_TYPE (exp));
1136 #ifdef CONSTANT_ALIGNMENT
1137 align = CONSTANT_ALIGNMENT (exp, align);
1138 #endif
1139 return align;
1142 /// EmitLV - Convert the specified l-value tree node to LLVM code, returning
1143 /// the address of the result.
1144 LValue TreeToLLVM::EmitLV(tree exp) {
1145 // Needs to be in sync with EmitVIEW_CONVERT_EXPR.
1146 LValue LV;
1148 switch (TREE_CODE(exp)) {
1149 default:
1150 errs() << "Unhandled lvalue expression!\n";
1151 debug_tree(exp);
1152 abort();
1154 case PARM_DECL:
1155 case VAR_DECL:
1156 case FUNCTION_DECL:
1157 case CONST_DECL:
1158 case RESULT_DECL:
1159 LV = EmitLV_DECL(exp);
1160 break;
1161 case ARRAY_RANGE_REF:
1162 case ARRAY_REF:
1163 LV = EmitLV_ARRAY_REF(exp);
1164 break;
1165 case COMPONENT_REF:
1166 LV = EmitLV_COMPONENT_REF(exp);
1167 break;
1168 case BIT_FIELD_REF:
1169 LV = EmitLV_BIT_FIELD_REF(exp);
1170 break;
1171 case REALPART_EXPR:
1172 LV = EmitLV_XXXXPART_EXPR(exp, 0);
1173 break;
1174 case IMAGPART_EXPR:
1175 LV = EmitLV_XXXXPART_EXPR(exp, 1);
1176 break;
1178 // Constants.
1179 case LABEL_DECL: {
1180 LV = LValue(EmitLV_LABEL_DECL(exp), 1);
1181 break;
1183 case COMPLEX_CST: {
1184 Value *Ptr = TreeConstantToLLVM::EmitLV_COMPLEX_CST(exp);
1185 LV = LValue(Ptr, get_constant_alignment(exp) / 8);
1186 break;
1188 case STRING_CST: {
1189 Value *Ptr = TreeConstantToLLVM::EmitLV_STRING_CST(exp);
1190 LV = LValue(Ptr, get_constant_alignment(exp) / 8);
1191 break;
1194 // Type Conversion.
1195 case VIEW_CONVERT_EXPR:
1196 LV = EmitLV_VIEW_CONVERT_EXPR(exp);
1197 break;
1199 // Exception Handling.
1200 case EXC_PTR_EXPR:
1201 LV = EmitLV_EXC_PTR_EXPR(exp);
1202 break;
1203 case FILTER_EXPR:
1204 LV = EmitLV_FILTER_EXPR(exp);
1205 break;
1207 // Trivial Cases.
1208 case WITH_SIZE_EXPR:
1209 LV = EmitLV_WITH_SIZE_EXPR(exp);
1210 break;
1211 case INDIRECT_REF:
1212 LV = EmitLV_INDIRECT_REF(exp);
1213 break;
1216 // Check that the type of the lvalue is indeed that of a pointer to the tree
1217 // node. This may not hold for bitfields because the type of a bitfield need
1218 // not match the type of the value being loaded out of it. Since LLVM has no
1219 // void* type, don't insist that void* be converted to a specific LLVM type.
1220 assert((LV.isBitfield() || VOID_TYPE_P(TREE_TYPE(exp)) ||
1221 LV.Ptr->getType() == ConvertType(TREE_TYPE(exp))->getPointerTo()) &&
1222 "LValue has wrong type!");
1224 return LV;
1227 //===----------------------------------------------------------------------===//
1228 // ... Utility Functions ...
1229 //===----------------------------------------------------------------------===//
1231 void TreeToLLVM::TODO(tree exp) {
1232 errs() << "Unhandled tree node\n";
1233 if (exp) debug_tree(exp);
1234 abort();
1237 /// CastToType - Cast the specified value to the specified type if it is
1238 /// not already that type.
1239 Value *TreeToLLVM::CastToType(unsigned opcode, Value *V, const Type* Ty) {
1240 // Handle 'trunc (zext i1 X to T2) to i1' as X, because this occurs all over
1241 // the place.
1242 if (ZExtInst *CI = dyn_cast<ZExtInst>(V))
1243 if (Ty->isInteger(1) && CI->getOperand(0)->getType()->isInteger(1))
1244 return CI->getOperand(0);
1246 return Builder.CreateCast(Instruction::CastOps(opcode), V, Ty,
1247 V->getName().data());
1250 /// CastToAnyType - Cast the specified value to the specified type making no
1251 /// assumptions about the types of the arguments. This creates an inferred cast.
1252 Value *TreeToLLVM::CastToAnyType(Value *V, bool VisSigned,
1253 const Type* Ty, bool TyIsSigned) {
1254 // Eliminate useless casts of a type to itself.
1255 if (V->getType() == Ty)
1256 return V;
1258 // The types are different so we must cast. Use getCastOpcode to create an
1259 // inferred cast opcode.
1260 Instruction::CastOps opc =
1261 CastInst::getCastOpcode(V, VisSigned, Ty, TyIsSigned);
1263 // Generate the cast and return it.
1264 return CastToType(opc, V, Ty);
1267 /// CastToUIntType - Cast the specified value to the specified type assuming
1268 /// that the value and type are unsigned integer types.
1269 Value *TreeToLLVM::CastToUIntType(Value *V, const Type* Ty) {
1270 // Eliminate useless casts of a type to itself.
1271 if (V->getType() == Ty)
1272 return V;
1274 unsigned SrcBits = V->getType()->getPrimitiveSizeInBits();
1275 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1276 assert(SrcBits != DstBits && "Types are different but have same #bits?");
1278 Instruction::CastOps opcode =
1279 (SrcBits > DstBits ? Instruction::Trunc : Instruction::ZExt);
1280 return CastToType(opcode, V, Ty);
1283 /// CastToSIntType - Cast the specified value to the specified type assuming
1284 /// that the value and type are signed integer types.
1285 Value *TreeToLLVM::CastToSIntType(Value *V, const Type* Ty) {
1286 // Eliminate useless casts of a type to itself.
1287 if (V->getType() == Ty)
1288 return V;
1290 unsigned SrcBits = V->getType()->getPrimitiveSizeInBits();
1291 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1292 assert(SrcBits != DstBits && "Types are different but have same #bits?");
1294 Instruction::CastOps opcode =
1295 (SrcBits > DstBits ? Instruction::Trunc : Instruction::SExt);
1296 return CastToType(opcode, V, Ty);
1299 /// CastToFPType - Cast the specified value to the specified type assuming
1300 /// that the value and type are floating point.
1301 Value *TreeToLLVM::CastToFPType(Value *V, const Type* Ty) {
1302 unsigned SrcBits = V->getType()->getPrimitiveSizeInBits();
1303 unsigned DstBits = Ty->getPrimitiveSizeInBits();
1304 if (SrcBits == DstBits)
1305 return V;
1306 Instruction::CastOps opcode = (SrcBits > DstBits ?
1307 Instruction::FPTrunc : Instruction::FPExt);
1308 return CastToType(opcode, V, Ty);
1311 /// BitCastToType - Insert a BitCast from V to Ty if needed. This is just a
1312 /// shorthand convenience function for CastToType(Instruction::BitCast,V,Ty).
1313 Value *TreeToLLVM::BitCastToType(Value *V, const Type *Ty) {
1314 return CastToType(Instruction::BitCast, V, Ty);
1317 /// CreateTemporary - Create a new alloca instruction of the specified type,
1318 /// inserting it into the entry block and returning it. The resulting
1319 /// instruction's type is a pointer to the specified type.
1320 AllocaInst *TreeToLLVM::CreateTemporary(const Type *Ty) {
1321 if (AllocaInsertionPoint == 0) {
1322 // Create a dummy instruction in the entry block as a marker to insert new
1323 // alloc instructions before. It doesn't matter what this instruction is,
1324 // it is dead. This allows us to insert allocas in order without having to
1325 // scan for an insertion point. Use BitCast for int -> int
1326 AllocaInsertionPoint = CastInst::Create(Instruction::BitCast,
1327 Constant::getNullValue(Type::getInt32Ty(Context)),
1328 Type::getInt32Ty(Context), "alloca point");
1329 // Insert it as the first instruction in the entry block.
1330 Fn->begin()->getInstList().insert(Fn->begin()->begin(),
1331 AllocaInsertionPoint);
1333 return new AllocaInst(Ty, 0, "memtmp", AllocaInsertionPoint);
1336 /// CreateTempLoc - Like CreateTemporary, but returns a MemRef.
1337 MemRef TreeToLLVM::CreateTempLoc(const Type *Ty) {
1338 AllocaInst *AI = CreateTemporary(Ty);
1339 // MemRefs do not allow alignment 0.
1340 if (!AI->getAlignment())
1341 AI->setAlignment(TD.getPrefTypeAlignment(Ty));
1342 return MemRef(AI, AI->getAlignment(), false);
1345 /// EmitBlock - Add the specified basic block to the end of the function. If
1346 /// the previous block falls through into it, add an explicit branch.
1347 void TreeToLLVM::EmitBlock(BasicBlock *BB) {
1348 BasicBlock *CurBB = Builder.GetInsertBlock();
1349 // If the previous block falls through to BB, add an explicit branch.
1350 if (CurBB->getTerminator() == 0) {
1351 // If the previous block has no label and is empty, remove it: it is a
1352 // post-terminator block.
1353 if (CurBB->getName().empty() && CurBB->begin() == CurBB->end())
1354 CurBB->eraseFromParent();
1355 else
1356 // Otherwise, fall through to this block.
1357 Builder.CreateBr(BB);
1360 // Add this block.
1361 Fn->getBasicBlockList().push_back(BB);
1362 Builder.SetInsertPoint(BB); // It is now the current block.
1365 /// CopyAggregate - Recursively traverse the potientially aggregate src/dest
1366 /// ptrs, copying all of the elements.
1367 static void CopyAggregate(MemRef DestLoc, MemRef SrcLoc,
1368 LLVMBuilder &Builder, tree gccType){
1369 assert(DestLoc.Ptr->getType() == SrcLoc.Ptr->getType() &&
1370 "Cannot copy between two pointers of different type!");
1371 const Type *ElTy =
1372 cast<PointerType>(DestLoc.Ptr->getType())->getElementType();
1374 unsigned Alignment = std::min(DestLoc.getAlignment(), SrcLoc.getAlignment());
1376 if (ElTy->isSingleValueType()) {
1377 LoadInst *V = Builder.CreateLoad(SrcLoc.Ptr, SrcLoc.Volatile);
1378 StoreInst *S = Builder.CreateStore(V, DestLoc.Ptr, DestLoc.Volatile);
1379 V->setAlignment(Alignment);
1380 S->setAlignment(Alignment);
1381 } else if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
1382 const StructLayout *SL = getTargetData().getStructLayout(STy);
1383 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1384 if (gccType && isPaddingElement(gccType, i))
1385 continue;
1386 Value *DElPtr = Builder.CreateStructGEP(DestLoc.Ptr, i);
1387 Value *SElPtr = Builder.CreateStructGEP(SrcLoc.Ptr, i);
1388 unsigned Align = MinAlign(Alignment, SL->getElementOffset(i));
1389 CopyAggregate(MemRef(DElPtr, Align, DestLoc.Volatile),
1390 MemRef(SElPtr, Align, SrcLoc.Volatile),
1391 Builder, 0);
1393 } else {
1394 const ArrayType *ATy = cast<ArrayType>(ElTy);
1395 unsigned EltSize = getTargetData().getTypeAllocSize(ATy->getElementType());
1396 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
1397 Value *DElPtr = Builder.CreateStructGEP(DestLoc.Ptr, i);
1398 Value *SElPtr = Builder.CreateStructGEP(SrcLoc.Ptr, i);
1399 unsigned Align = MinAlign(Alignment, i * EltSize);
1400 CopyAggregate(MemRef(DElPtr, Align, DestLoc.Volatile),
1401 MemRef(SElPtr, Align, SrcLoc.Volatile),
1402 Builder, 0);
1407 /// CountAggregateElements - Return the number of elements in the specified type
1408 /// that will need to be loaded/stored if we copy this by explicit accesses.
1409 static unsigned CountAggregateElements(const Type *Ty) {
1410 if (Ty->isSingleValueType()) return 1;
1412 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1413 unsigned NumElts = 0;
1414 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i)
1415 NumElts += CountAggregateElements(STy->getElementType(i));
1416 return NumElts;
1417 } else {
1418 const ArrayType *ATy = cast<ArrayType>(Ty);
1419 return ATy->getNumElements()*CountAggregateElements(ATy->getElementType());
1423 /// containsFPField - indicates whether the given LLVM type
1424 /// contains any floating point elements.
1426 static bool containsFPField(const Type *LLVMTy) {
1427 if (LLVMTy->isFloatingPoint())
1428 return true;
1429 const StructType* STy = dyn_cast<StructType>(LLVMTy);
1430 if (STy) {
1431 for (StructType::element_iterator I = STy->element_begin(),
1432 E = STy->element_end(); I != E; I++) {
1433 const Type *Ty = *I;
1434 if (Ty->isFloatingPoint())
1435 return true;
1436 if (isa<StructType>(Ty) && containsFPField(Ty))
1437 return true;
1438 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1439 if (ATy && containsFPField(ATy->getElementType()))
1440 return true;
1441 const VectorType *VTy = dyn_cast<VectorType>(Ty);
1442 if (VTy && containsFPField(VTy->getElementType()))
1443 return true;
1446 return false;
1449 #ifndef TARGET_LLVM_MIN_BYTES_COPY_BY_MEMCPY
1450 #define TARGET_LLVM_MIN_BYTES_COPY_BY_MEMCPY 64
1451 #endif
1453 /// EmitAggregateCopy - Copy the elements from SrcLoc to DestLoc, using the
1454 /// GCC type specified by GCCType to know which elements to copy.
1455 void TreeToLLVM::EmitAggregateCopy(MemRef DestLoc, MemRef SrcLoc, tree type) {
1456 if (DestLoc.Ptr == SrcLoc.Ptr && !DestLoc.Volatile && !SrcLoc.Volatile)
1457 return; // noop copy.
1459 // If the type is small, copy the elements instead of using a block copy.
1460 const Type *LLVMTy = ConvertType(type);
1461 unsigned NumElts = CountAggregateElements(LLVMTy);
1462 if (TREE_CODE(TYPE_SIZE(type)) == INTEGER_CST &&
1463 (NumElts == 1 ||
1464 TREE_INT_CST_LOW(TYPE_SIZE_UNIT(type)) <
1465 TARGET_LLVM_MIN_BYTES_COPY_BY_MEMCPY)) {
1467 // Some targets (x87) cannot pass non-floating-point values using FP
1468 // instructions. The LLVM type for a union may include FP elements,
1469 // even if some of the union fields do not; it is unsafe to pass such
1470 // converted types element by element. PR 2680.
1472 // If the GCC type is not fully covered by the LLVM type, use memcpy. This
1473 // can occur with unions etc.
1474 if ((TREE_CODE(type) != UNION_TYPE || !containsFPField(LLVMTy)) &&
1475 !TheTypeConverter->GCCTypeOverlapsWithLLVMTypePadding(type, LLVMTy) &&
1476 // Don't copy tons of tiny elements.
1477 NumElts <= 8) {
1478 DestLoc.Ptr = BitCastToType(DestLoc.Ptr,
1479 LLVMTy->getPointerTo());
1480 SrcLoc.Ptr = BitCastToType(SrcLoc.Ptr,
1481 LLVMTy->getPointerTo());
1482 CopyAggregate(DestLoc, SrcLoc, Builder, type);
1483 return;
1487 Value *TypeSize = Emit(TYPE_SIZE_UNIT(type), 0);
1488 EmitMemCpy(DestLoc.Ptr, SrcLoc.Ptr, TypeSize,
1489 std::min(DestLoc.getAlignment(), SrcLoc.getAlignment()));
1492 /// ZeroAggregate - Recursively traverse the potentially aggregate DestLoc,
1493 /// zero'ing all of the elements.
1494 static void ZeroAggregate(MemRef DestLoc, LLVMBuilder &Builder) {
1495 const Type *ElTy =
1496 cast<PointerType>(DestLoc.Ptr->getType())->getElementType();
1497 if (ElTy->isSingleValueType()) {
1498 StoreInst *St = Builder.CreateStore(Constant::getNullValue(ElTy),
1499 DestLoc.Ptr, DestLoc.Volatile);
1500 St->setAlignment(DestLoc.getAlignment());
1501 } else if (const StructType *STy = dyn_cast<StructType>(ElTy)) {
1502 const StructLayout *SL = getTargetData().getStructLayout(STy);
1503 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
1504 Value *Ptr = Builder.CreateStructGEP(DestLoc.Ptr, i);
1505 unsigned Alignment = MinAlign(DestLoc.getAlignment(),
1506 SL->getElementOffset(i));
1507 ZeroAggregate(MemRef(Ptr, Alignment, DestLoc.Volatile), Builder);
1509 } else {
1510 const ArrayType *ATy = cast<ArrayType>(ElTy);
1511 unsigned EltSize = getTargetData().getTypeAllocSize(ATy->getElementType());
1512 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
1513 Value *Ptr = Builder.CreateStructGEP(DestLoc.Ptr, i);
1514 unsigned Alignment = MinAlign(DestLoc.getAlignment(), i * EltSize);
1515 ZeroAggregate(MemRef(Ptr, Alignment, DestLoc.Volatile), Builder);
1520 /// EmitAggregateZero - Zero the elements of DestLoc.
1522 void TreeToLLVM::EmitAggregateZero(MemRef DestLoc, tree type) {
1523 // If the type is small, copy the elements instead of using a block copy.
1524 if (TREE_CODE(TYPE_SIZE(type)) == INTEGER_CST &&
1525 TREE_INT_CST_LOW(TYPE_SIZE_UNIT(type)) < 128) {
1526 const Type *LLVMTy = ConvertType(type);
1528 // If the GCC type is not fully covered by the LLVM type, use memset. This
1529 // can occur with unions etc.
1530 if (!TheTypeConverter->GCCTypeOverlapsWithLLVMTypePadding(type, LLVMTy) &&
1531 // Don't zero tons of tiny elements.
1532 CountAggregateElements(LLVMTy) <= 8) {
1533 DestLoc.Ptr = BitCastToType(DestLoc.Ptr,
1534 LLVMTy->getPointerTo());
1535 ZeroAggregate(DestLoc, Builder);
1536 return;
1540 EmitMemSet(DestLoc.Ptr, ConstantInt::get(Type::getInt8Ty(Context), 0),
1541 Emit(TYPE_SIZE_UNIT(type), 0), DestLoc.getAlignment());
1544 Value *TreeToLLVM::EmitMemCpy(Value *DestPtr, Value *SrcPtr, Value *Size,
1545 unsigned Align) {
1546 const Type *SBP = Type::getInt8PtrTy(Context);
1547 const Type *IntPtr = TD.getIntPtrType(Context);
1548 Value *Ops[4] = {
1549 BitCastToType(DestPtr, SBP),
1550 BitCastToType(SrcPtr, SBP),
1551 CastToSIntType(Size, IntPtr),
1552 ConstantInt::get(Type::getInt32Ty(Context), Align)
1555 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, Intrinsic::memcpy,
1556 &IntPtr, 1), Ops, Ops+4);
1557 return Ops[0];
1560 Value *TreeToLLVM::EmitMemMove(Value *DestPtr, Value *SrcPtr, Value *Size,
1561 unsigned Align) {
1562 const Type *SBP = Type::getInt8PtrTy(Context);
1563 const Type *IntPtr = TD.getIntPtrType(Context);
1564 Value *Ops[4] = {
1565 BitCastToType(DestPtr, SBP),
1566 BitCastToType(SrcPtr, SBP),
1567 CastToSIntType(Size, IntPtr),
1568 ConstantInt::get(Type::getInt32Ty(Context), Align)
1571 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, Intrinsic::memmove,
1572 &IntPtr, 1), Ops, Ops+4);
1573 return Ops[0];
1576 Value *TreeToLLVM::EmitMemSet(Value *DestPtr, Value *SrcVal, Value *Size,
1577 unsigned Align) {
1578 const Type *SBP = Type::getInt8PtrTy(Context);
1579 const Type *IntPtr = TD.getIntPtrType(Context);
1580 Value *Ops[4] = {
1581 BitCastToType(DestPtr, SBP),
1582 CastToSIntType(SrcVal, Type::getInt8Ty(Context)),
1583 CastToSIntType(Size, IntPtr),
1584 ConstantInt::get(Type::getInt32Ty(Context), Align)
1587 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, Intrinsic::memset,
1588 &IntPtr, 1), Ops, Ops+4);
1589 return Ops[0];
1593 // Emits code to do something for a type attribute
1594 void TreeToLLVM::EmitTypeGcroot(Value *V, tree decl) {
1595 // GC intrinsics can only be used in functions which specify a collector.
1596 Fn->setGC("shadow-stack");
1598 Function *gcrootFun = Intrinsic::getDeclaration(TheModule,
1599 Intrinsic::gcroot);
1601 // The idea is that it's a pointer to type "Value"
1602 // which is opaque* but the routine expects i8** and i8*.
1603 const PointerType *Ty = Type::getInt8PtrTy(Context);
1604 V = Builder.CreateBitCast(V, Ty->getPointerTo());
1606 Value *Ops[2] = {
1608 ConstantPointerNull::get(Ty)
1611 Builder.CreateCall(gcrootFun, Ops, Ops+2);
1614 // Emits annotate intrinsic if the decl has the annotate attribute set.
1615 void TreeToLLVM::EmitAnnotateIntrinsic(Value *V, tree decl) {
1617 // Handle annotate attribute on global.
1618 tree annotateAttr = lookup_attribute("annotate", DECL_ATTRIBUTES (decl));
1620 if (!annotateAttr)
1621 return;
1623 Function *annotateFun = Intrinsic::getDeclaration(TheModule,
1624 Intrinsic::var_annotation);
1626 // Get file and line number
1627 Constant *lineNo =
1628 ConstantInt::get(Type::getInt32Ty(Context), DECL_SOURCE_LINE(decl));
1629 Constant *file = ConvertMetadataStringToGV(DECL_SOURCE_FILE(decl));
1630 const Type *SBP= Type::getInt8PtrTy(Context);
1631 file = Builder.getFolder().CreateBitCast(file, SBP);
1633 // There may be multiple annotate attributes. Pass return of lookup_attr
1634 // to successive lookups.
1635 while (annotateAttr) {
1637 // Each annotate attribute is a tree list.
1638 // Get value of list which is our linked list of args.
1639 tree args = TREE_VALUE(annotateAttr);
1641 // Each annotate attribute may have multiple args.
1642 // Treat each arg as if it were a separate annotate attribute.
1643 for (tree a = args; a; a = TREE_CHAIN(a)) {
1644 // Each element of the arg list is a tree list, so get value
1645 tree val = TREE_VALUE(a);
1647 // Assert its a string, and then get that string.
1648 assert(TREE_CODE(val) == STRING_CST &&
1649 "Annotate attribute arg should always be a string");
1650 const Type *SBP = Type::getInt8PtrTy(Context);
1651 Constant *strGV = TreeConstantToLLVM::EmitLV_STRING_CST(val);
1652 Value *Ops[4] = {
1653 BitCastToType(V, SBP),
1654 BitCastToType(strGV, SBP),
1655 file,
1656 lineNo
1659 Builder.CreateCall(annotateFun, Ops, Ops+4);
1662 // Get next annotate attribute.
1663 annotateAttr = TREE_CHAIN(annotateAttr);
1664 if (annotateAttr)
1665 annotateAttr = lookup_attribute("annotate", annotateAttr);
1669 //===----------------------------------------------------------------------===//
1670 // ... Basic Lists and Binding Scopes ...
1671 //===----------------------------------------------------------------------===//
1673 /// EmitAutomaticVariableDecl - Emit the function-local decl to the current
1674 /// function and set DECL_LLVM for the decl to the right pointer.
1675 void TreeToLLVM::EmitAutomaticVariableDecl(tree decl) {
1676 tree type = TREE_TYPE(decl);
1678 // An LLVM value pointer for this decl may already be set, for example, if the
1679 // named return value optimization is being applied to this function, and
1680 // this variable is the one being returned.
1681 assert(!DECL_LLVM_SET_P(decl) && "Shouldn't call this on an emitted var!");
1683 // For a CONST_DECL, set mode, alignment, and sizes from those of the
1684 // type in case this node is used in a reference.
1685 if (TREE_CODE(decl) == CONST_DECL) {
1686 DECL_MODE(decl) = TYPE_MODE(type);
1687 DECL_ALIGN(decl) = TYPE_ALIGN(type);
1688 DECL_SIZE(decl) = TYPE_SIZE(type);
1689 DECL_SIZE_UNIT(decl) = TYPE_SIZE_UNIT(type);
1690 return;
1693 // Otherwise, only automatic (and result) variables need any expansion done.
1694 // Static and external variables, and external functions, will be handled by
1695 // `assemble_variable' (called from finish_decl). TYPE_DECL requires nothing.
1696 // PARM_DECLs are handled in `llvm_expand_function_start'.
1697 if ((TREE_CODE(decl) != VAR_DECL && TREE_CODE(decl) != RESULT_DECL) ||
1698 TREE_STATIC(decl) || DECL_EXTERNAL(decl) || type == error_mark_node)
1699 return;
1701 // Gimple temporaries are handled specially: their DECL_LLVM is set when the
1702 // definition is encountered.
1703 if (isGimpleTemporary(decl))
1704 return;
1706 // If this is just the rotten husk of a variable that the gimplifier
1707 // eliminated all uses of, but is preserving for debug info, ignore it.
1708 if (TREE_CODE(decl) == VAR_DECL && DECL_VALUE_EXPR(decl))
1709 return;
1711 const Type *Ty; // Type to allocate
1712 Value *Size = 0; // Amount to alloca (null for 1)
1714 if (DECL_SIZE(decl) == 0) { // Variable with incomplete type.
1715 if (DECL_INITIAL(decl) == 0)
1716 return; // Error message was already done; now avoid a crash.
1717 else {
1718 // "An initializer is going to decide the size of this array."??
1719 TODO(decl);
1720 abort();
1722 } else if (TREE_CODE(DECL_SIZE_UNIT(decl)) == INTEGER_CST) {
1723 // Variable of fixed size that goes on the stack.
1724 Ty = ConvertType(type);
1725 } else {
1726 // Dynamic-size object: must push space on the stack.
1727 if (TREE_CODE(type) == ARRAY_TYPE
1728 && isSequentialCompatible(type)
1729 && TYPE_SIZE(type) == DECL_SIZE(decl)) {
1730 Ty = ConvertType(TREE_TYPE(type)); // Get array element type.
1731 // Compute the number of elements in the array.
1732 Size = Emit(DECL_SIZE(decl), 0);
1733 assert(!integer_zerop(TYPE_SIZE(TREE_TYPE(type)))
1734 && "Array of positive size with elements of zero size!");
1735 Value *EltSize = Emit(TYPE_SIZE(TREE_TYPE(type)), 0);
1736 Size = Builder.CreateUDiv(Size, EltSize, "len");
1737 } else {
1738 // Compute the variable's size in bytes.
1739 Size = Emit(DECL_SIZE_UNIT(decl), 0);
1740 Ty = Type::getInt8Ty(Context);
1742 Size = CastToUIntType(Size, Type::getInt32Ty(Context));
1745 unsigned Alignment = 0; // Alignment in bytes.
1747 // Set the alignment for the local if one of the following condition is met
1748 // 1) DECL_ALIGN is better than the alignment as per ABI specification
1749 // 2) DECL_ALIGN is set by user.
1750 if (DECL_ALIGN(decl)) {
1751 unsigned TargetAlign = getTargetData().getABITypeAlignment(Ty);
1752 if (DECL_USER_ALIGN(decl) || 8 * TargetAlign < (unsigned)DECL_ALIGN(decl))
1753 Alignment = DECL_ALIGN(decl) / 8;
1756 const char *Name; // Name of variable
1757 if (DECL_NAME(decl))
1758 Name = IDENTIFIER_POINTER(DECL_NAME(decl));
1759 else if (TREE_CODE(decl) == RESULT_DECL)
1760 Name = "retval";
1761 else
1762 Name = "";
1764 // Insert an alloca for this variable.
1765 AllocaInst *AI;
1766 if (!Size) { // Fixed size alloca -> entry block.
1767 AI = CreateTemporary(Ty);
1768 AI->setName(Name);
1769 } else {
1770 AI = Builder.CreateAlloca(Ty, Size, Name);
1773 AI->setAlignment(Alignment);
1775 SET_DECL_LLVM(decl, AI);
1777 // Handle annotate attributes
1778 if (DECL_ATTRIBUTES(decl))
1779 EmitAnnotateIntrinsic(AI, decl);
1781 // Handle gcroot attribute
1782 if (POINTER_TYPE_P(TREE_TYPE (decl))
1783 && lookup_attribute("gcroot", TYPE_ATTRIBUTES(TREE_TYPE (decl))))
1785 // We should null out local variables so that a stack crawl
1786 // before initialization doesn't get garbage results to follow.
1787 const Type *T = cast<PointerType>(AI->getType())->getElementType();
1788 EmitTypeGcroot(AI, decl);
1789 Builder.CreateStore(Constant::getNullValue(T), AI);
1792 if (TheDebugInfo) {
1793 if (DECL_NAME(decl)) {
1794 TheDebugInfo->EmitDeclare(decl, dwarf::DW_TAG_auto_variable,
1795 Name, TREE_TYPE(decl), AI,
1796 Builder);
1797 } else if (TREE_CODE(decl) == RESULT_DECL) {
1798 TheDebugInfo->EmitDeclare(decl, dwarf::DW_TAG_return_variable,
1799 Name, TREE_TYPE(decl), AI,
1800 Builder);
1805 //===----------------------------------------------------------------------===//
1806 // ... Control Flow ...
1807 //===----------------------------------------------------------------------===//
1809 /// EmitLABEL_EXPR - Emit the basic block corresponding to the specified label.
1811 Value *TreeToLLVM::EmitLABEL_EXPR(tree exp) {
1812 EmitBlock(getLabelDeclBlock(TREE_OPERAND(exp, 0)));
1813 return 0;
1816 Value *TreeToLLVM::EmitGOTO_EXPR(tree exp) {
1817 tree dest = GOTO_DESTINATION(exp);
1818 if (TREE_CODE(dest) == LABEL_DECL) {
1819 // Direct branch.
1820 Builder.CreateBr(getLabelDeclBlock(dest));
1821 } else {
1822 // Indirect branch.
1823 basic_block bb = bb_for_stmt(exp);
1824 Value *V = Emit(dest, 0);
1825 IndirectBrInst *Br = Builder.CreateIndirectBr(V, EDGE_COUNT(bb->succs));
1827 // Add the list of possible destinations.
1828 edge e;
1829 edge_iterator ei;
1830 FOR_EACH_EDGE (e, ei, bb->succs)
1831 Br->addDestination(getLabelDeclBlock(tree_block_label(e->dest)));
1833 EmitBlock(BasicBlock::Create(Context, ""));
1834 return 0;
1838 Value *TreeToLLVM::EmitRETURN_EXPR(tree exp, const MemRef *DestLoc) {
1839 assert(DestLoc == 0 && "Does not return a value!");
1840 tree retval = TREE_OPERAND(exp, 0);
1842 assert((!retval || TREE_CODE(retval) == RESULT_DECL ||
1843 ((TREE_CODE(retval) == MODIFY_EXPR
1844 || TREE_CODE(retval) == INIT_EXPR) &&
1845 TREE_CODE(TREE_OPERAND(retval, 0)) == RESULT_DECL)) &&
1846 "RETURN_EXPR not gimple!");
1848 if (retval && TREE_CODE(retval) != RESULT_DECL)
1849 // Emit the assignment to RESULT_DECL.
1850 Emit(retval, 0);
1852 // Emit a branch to the exit label.
1853 Builder.CreateBr(ReturnBB);
1854 EmitBlock(BasicBlock::Create(Context, ""));
1855 return 0;
1858 Value *TreeToLLVM::EmitCOND_EXPR(tree exp) {
1859 tree exp_cond = COND_EXPR_COND(exp);
1861 // Emit the conditional expression. Special case comparisons since they are
1862 // very common and we want to avoid an extension to 'int' of the intermediate
1863 // result.
1864 unsigned UIPred = 0, SIPred = 0, FPPred = ~0;
1865 Value *Cond;
1866 switch (TREE_CODE(exp_cond)) {
1867 default: break;
1868 case LT_EXPR:
1869 UIPred = ICmpInst::ICMP_ULT;
1870 SIPred = ICmpInst::ICMP_SLT;
1871 FPPred = FCmpInst::FCMP_OLT;
1872 break;
1873 case LE_EXPR:
1874 UIPred = ICmpInst::ICMP_ULE;
1875 SIPred = ICmpInst::ICMP_SLE;
1876 FPPred = FCmpInst::FCMP_OLE;
1877 break;
1878 case GT_EXPR:
1879 UIPred = ICmpInst::ICMP_UGT;
1880 SIPred = ICmpInst::ICMP_SGT;
1881 FPPred = FCmpInst::FCMP_OGT;
1882 break;
1883 case GE_EXPR:
1884 UIPred = ICmpInst::ICMP_UGE;
1885 SIPred = ICmpInst::ICMP_SGE;
1886 FPPred = FCmpInst::FCMP_OGE;
1887 break;
1888 case EQ_EXPR:
1889 UIPred = SIPred = ICmpInst::ICMP_EQ;
1890 FPPred = FCmpInst::FCMP_OEQ;
1891 break;
1892 case NE_EXPR:
1893 UIPred = SIPred = ICmpInst::ICMP_NE;
1894 FPPred = FCmpInst::FCMP_UNE;
1895 break;
1896 case UNORDERED_EXPR: FPPred = FCmpInst::FCMP_UNO; break;
1897 case ORDERED_EXPR: FPPred = FCmpInst::FCMP_ORD; break;
1898 case UNLT_EXPR: FPPred = FCmpInst::FCMP_ULT; break;
1899 case UNLE_EXPR: FPPred = FCmpInst::FCMP_ULE; break;
1900 case UNGT_EXPR: FPPred = FCmpInst::FCMP_UGT; break;
1901 case UNGE_EXPR: FPPred = FCmpInst::FCMP_UGE; break;
1902 case UNEQ_EXPR: FPPred = FCmpInst::FCMP_UEQ; break;
1903 case LTGT_EXPR: FPPred = FCmpInst::FCMP_ONE; break;
1906 // If the operand wasn't a compare, emit it fully generally. If it was, emit
1907 // it with EmitCompare to get the result as an i1.
1908 if (FPPred == ~0U) {
1909 Cond = Emit(exp_cond, 0);
1910 // Comparison against zero to convert the result to i1.
1911 if (!Cond->getType()->isInteger(1))
1912 Cond = Builder.CreateIsNotNull(Cond, "toBool");
1913 } else {
1914 Cond = EmitCompare(exp_cond, UIPred, SIPred, FPPred, Type::getInt1Ty(Context));
1915 assert(Cond->getType() == Type::getInt1Ty(Context));
1918 tree Then = COND_EXPR_THEN(exp);
1919 tree Else = COND_EXPR_ELSE(exp);
1920 assert(TREE_CODE(Then) == GOTO_EXPR && TREE_CODE(Else) == GOTO_EXPR
1921 && "Not a gimple if?");
1923 BasicBlock *ThenDest = getLabelDeclBlock(TREE_OPERAND(Then, 0));
1924 BasicBlock *ElseDest = getLabelDeclBlock(TREE_OPERAND(Else, 0));
1925 Builder.CreateCondBr(Cond, ThenDest, ElseDest);
1926 EmitBlock(BasicBlock::Create(Context, ""));
1927 return 0;
1930 Value *TreeToLLVM::EmitSWITCH_EXPR(tree exp) {
1931 tree Cases = SWITCH_LABELS(exp);
1933 // Emit the condition.
1934 Value *SwitchExp = Emit(SWITCH_COND(exp), 0);
1935 bool ExpIsSigned = !TYPE_UNSIGNED(TREE_TYPE(SWITCH_COND(exp)));
1937 // Emit the switch instruction.
1938 SwitchInst *SI = Builder.CreateSwitch(SwitchExp, Builder.GetInsertBlock(),
1939 TREE_VEC_LENGTH(Cases));
1940 EmitBlock(BasicBlock::Create(Context, ""));
1941 // Default location starts out as fall-through
1942 SI->setSuccessor(0, Builder.GetInsertBlock());
1944 assert(!SWITCH_BODY(exp) && "not a gimple switch?");
1946 BasicBlock *DefaultDest = NULL;
1947 for (unsigned i = 0, e = TREE_VEC_LENGTH(Cases); i != e; ++i) {
1948 BasicBlock *Dest = getLabelDeclBlock(CASE_LABEL(TREE_VEC_ELT(Cases, i)));
1950 tree low = CASE_LOW(TREE_VEC_ELT(Cases, i));
1951 if (!low) {
1952 DefaultDest = Dest;
1953 continue;
1956 // Convert the integer to the right type.
1957 Value *Val = Emit(low, 0);
1958 Val = CastToAnyType(Val, !TYPE_UNSIGNED(TREE_TYPE(low)),
1959 SwitchExp->getType(), ExpIsSigned);
1960 ConstantInt *LowC = cast<ConstantInt>(Val);
1962 tree high = CASE_HIGH(TREE_VEC_ELT(Cases, i));
1963 if (!high) {
1964 SI->addCase(LowC, Dest); // Single destination.
1965 continue;
1968 // Otherwise, we have a range, like 'case 1 ... 17'.
1969 Val = Emit(high, 0);
1970 // Make sure the case value is the same type as the switch expression
1971 Val = CastToAnyType(Val, !TYPE_UNSIGNED(TREE_TYPE(high)),
1972 SwitchExp->getType(), ExpIsSigned);
1973 ConstantInt *HighC = cast<ConstantInt>(Val);
1975 APInt Range = HighC->getValue() - LowC->getValue();
1976 if (Range.ult(APInt(Range.getBitWidth(), 64))) {
1977 // Add all of the necessary successors to the switch.
1978 APInt CurrentValue = LowC->getValue();
1979 while (1) {
1980 SI->addCase(LowC, Dest);
1981 if (LowC == HighC) break; // Emitted the last one.
1982 CurrentValue++;
1983 LowC = ConstantInt::get(Context, CurrentValue);
1985 } else {
1986 // The range is too big to add to the switch - emit an "if".
1987 Value *Diff = Builder.CreateSub(SwitchExp, LowC);
1988 Value *Cond = Builder.CreateICmpULE(Diff,
1989 ConstantInt::get(Context, Range));
1990 BasicBlock *False_Block = BasicBlock::Create(Context, "case_false");
1991 Builder.CreateCondBr(Cond, Dest, False_Block);
1992 EmitBlock(False_Block);
1996 if (DefaultDest) {
1997 if (SI->getSuccessor(0) == Builder.GetInsertBlock())
1998 SI->setSuccessor(0, DefaultDest);
1999 else {
2000 Builder.CreateBr(DefaultDest);
2001 // Emit a "fallthrough" block, which is almost certainly dead.
2002 EmitBlock(BasicBlock::Create(Context, ""));
2006 return 0;
2010 /// CreateExceptionValues - Create values used internally by exception handling.
2011 void TreeToLLVM::CreateExceptionValues() {
2012 // Check to see if the exception values have been constructed.
2013 if (ExceptionValue) return;
2015 ExceptionValue = CreateTemporary(Type::getInt8PtrTy(Context));
2016 ExceptionValue->setName("eh_exception");
2018 ExceptionSelectorValue = CreateTemporary(Type::getInt32Ty(Context));
2019 ExceptionSelectorValue->setName("eh_selector");
2021 FuncEHException = Intrinsic::getDeclaration(TheModule,
2022 Intrinsic::eh_exception);
2023 FuncEHSelector = Intrinsic::getDeclaration(TheModule,
2024 Intrinsic::eh_selector);
2025 FuncEHGetTypeID = Intrinsic::getDeclaration(TheModule,
2026 Intrinsic::eh_typeid_for);
2029 /// getPostPad - Return the post landing pad for the given exception handling
2030 /// region, creating it if necessary.
2031 BasicBlock *TreeToLLVM::getPostPad(unsigned RegionNo) {
2032 PostPads.grow(RegionNo);
2033 BasicBlock *&PostPad = PostPads[RegionNo];
2035 if (!PostPad)
2036 PostPad = BasicBlock::Create(Context, "ppad");
2038 return PostPad;
2041 /// AddHandler - Append the given region to a vector of exception handlers.
2042 /// A callback passed to foreach_reachable_handler.
2043 static void AddHandler (struct eh_region *region, void *data) {
2044 ((std::vector<struct eh_region *> *)data)->push_back(region);
2047 /// EmitLandingPads - Emit EH landing pads.
2048 void TreeToLLVM::EmitLandingPads() {
2049 std::vector<Value*> Args;
2050 std::vector<struct eh_region *> Handlers;
2052 for (unsigned i = 1; i < LandingPads.size(); ++i) {
2053 BasicBlock *LandingPad = LandingPads[i];
2055 if (!LandingPad)
2056 continue;
2058 CreateExceptionValues();
2060 EmitBlock(LandingPad);
2062 // Fetch and store the exception.
2063 Value *Ex = Builder.CreateCall(FuncEHException, "eh_ptr");
2064 Builder.CreateStore(Ex, ExceptionValue);
2066 // Fetch and store the exception selector.
2068 // The exception and the personality function.
2069 Args.push_back(Builder.CreateLoad(ExceptionValue, "eh_ptr"));
2070 assert(llvm_eh_personality_libfunc
2071 && "no exception handling personality function!");
2072 Args.push_back(BitCastToType(DECL_LLVM(llvm_eh_personality_libfunc),
2073 Type::getInt8PtrTy(Context)));
2075 // Add selections for each handler.
2076 foreach_reachable_handler(i, false, AddHandler, &Handlers);
2078 for (std::vector<struct eh_region *>::iterator I = Handlers.begin(),
2079 E = Handlers.end(); I != E; ++I) {
2080 struct eh_region *region = *I;
2082 // Create a post landing pad for the handler.
2083 getPostPad(get_eh_region_number(region));
2085 int RegionKind = classify_eh_handler(region);
2086 if (RegionKind < 0) {
2087 // Filter - note the length.
2088 tree TypeList = get_eh_type_list(region);
2089 unsigned Length = list_length(TypeList);
2090 Args.reserve(Args.size() + Length + 1);
2091 Args.push_back(ConstantInt::get(Type::getInt32Ty(Context), Length + 1));
2093 // Add the type infos.
2094 for (; TypeList; TypeList = TREE_CHAIN(TypeList)) {
2095 tree TType = lookup_type_for_runtime(TREE_VALUE(TypeList));
2096 Args.push_back(Emit(TType, 0));
2098 } else if (RegionKind > 0) {
2099 // Catch.
2100 tree TypeList = get_eh_type_list(region);
2102 if (!TypeList) {
2103 // Catch-all - push a null pointer.
2104 Args.push_back(
2105 Constant::getNullValue(Type::getInt8PtrTy(Context))
2107 } else {
2108 // Add the type infos.
2109 for (; TypeList; TypeList = TREE_CHAIN(TypeList)) {
2110 tree TType = lookup_type_for_runtime(TREE_VALUE(TypeList));
2111 Args.push_back(Emit(TType, 0));
2117 if (can_throw_external_1(i, false)) {
2118 // Some exceptions from this region may not be caught by any handler.
2119 // Since invokes are required to branch to the unwind label no matter
2120 // what exception is being unwound, append a catch-all.
2122 // The representation of a catch-all is language specific.
2123 Value *CatchAll;
2124 if (USING_SJLJ_EXCEPTIONS || !lang_eh_catch_all) {
2125 // Use a "cleanup" - this should be good enough for most languages.
2126 CatchAll = ConstantInt::get(Type::getInt32Ty(Context), 0);
2127 } else {
2128 tree catch_all_type = lang_eh_catch_all();
2129 if (catch_all_type == NULL_TREE)
2130 // Use a C++ style null catch-all object.
2131 CatchAll = Constant::getNullValue(
2132 Type::getInt8PtrTy(Context));
2133 else
2134 // This language has a type that catches all others.
2135 CatchAll = Emit(catch_all_type, 0);
2137 Args.push_back(CatchAll);
2140 // Emit the selector call.
2141 Value *Select = Builder.CreateCall(FuncEHSelector, Args.begin(), Args.end(),
2142 "eh_select");
2143 Builder.CreateStore(Select, ExceptionSelectorValue);
2144 // Branch to the post landing pad for the first reachable handler.
2145 assert(!Handlers.empty() && "Landing pad but no handler?");
2146 Builder.CreateBr(getPostPad(get_eh_region_number(*Handlers.begin())));
2148 Handlers.clear();
2149 Args.clear();
2153 /// EmitPostPads - Emit EH post landing pads.
2154 void TreeToLLVM::EmitPostPads() {
2155 std::vector<struct eh_region *> Handlers;
2157 for (unsigned i = 1; i < PostPads.size(); ++i) {
2158 BasicBlock *PostPad = PostPads[i];
2160 if (!PostPad)
2161 continue;
2163 CreateExceptionValues();
2165 EmitBlock(PostPad);
2167 struct eh_region *region = get_eh_region(i);
2168 BasicBlock *Dest = getLabelDeclBlock(get_eh_region_tree_label(region));
2170 int RegionKind = classify_eh_handler(region);
2171 if (!RegionKind || !get_eh_type_list(region)) {
2172 // Cleanup, catch-all or empty filter - no testing required.
2173 Builder.CreateBr(Dest);
2174 continue;
2175 } else if (RegionKind < 0) {
2176 // Filter - the result of a filter selection will be a negative index if
2177 // there is a match.
2178 Value *Select = Builder.CreateLoad(ExceptionSelectorValue);
2180 // Compare with the filter action value.
2181 Value *Zero = ConstantInt::get(Select->getType(), 0);
2182 Value *Compare = Builder.CreateICmpSLT(Select, Zero);
2184 // Branch on the compare.
2185 BasicBlock *NoFilterBB = BasicBlock::Create(Context, "nofilter");
2186 Builder.CreateCondBr(Compare, Dest, NoFilterBB);
2187 EmitBlock(NoFilterBB);
2188 } else if (RegionKind > 0) {
2189 // Catch
2190 tree TypeList = get_eh_type_list(region);
2192 Value *Cond = NULL;
2193 for (; TypeList; TypeList = TREE_CHAIN (TypeList)) {
2194 Value *TType = Emit(lookup_type_for_runtime(TREE_VALUE(TypeList)), 0);
2195 TType = BitCastToType(TType,
2196 Type::getInt8PtrTy(Context));
2198 // Call get eh type id.
2199 Value *TypeID = Builder.CreateCall(FuncEHGetTypeID, TType, "eh_typeid");
2200 Value *Select = Builder.CreateLoad(ExceptionSelectorValue);
2202 // Compare with the exception selector.
2203 Value *Compare = Builder.CreateICmpEQ(Select, TypeID);
2205 Cond = Cond ? Builder.CreateOr(Cond, Compare) : Compare;
2208 BasicBlock *NoCatchBB = NULL;
2210 // If the comparion fails, branch to the next catch that has a
2211 // post landing pad.
2212 struct eh_region *next_catch = get_eh_next_catch(region);
2213 for (; next_catch; next_catch = get_eh_next_catch(next_catch)) {
2214 unsigned CatchNo = get_eh_region_number(next_catch);
2216 if (CatchNo < PostPads.size())
2217 NoCatchBB = PostPads[CatchNo];
2219 if (NoCatchBB)
2220 break;
2223 if (NoCatchBB) {
2224 // Branch on the compare.
2225 Builder.CreateCondBr(Cond, Dest, NoCatchBB);
2226 continue;
2229 // If there is no such catch, execute a RESX if the comparison fails.
2230 NoCatchBB = BasicBlock::Create(Context, "nocatch");
2231 // Branch on the compare.
2232 Builder.CreateCondBr(Cond, Dest, NoCatchBB);
2233 EmitBlock(NoCatchBB);
2236 // Emit a RESX_EXPR which skips handlers with no post landing pad.
2237 foreach_reachable_handler(i, true, AddHandler, &Handlers);
2239 BasicBlock *TargetBB = NULL;
2241 for (std::vector<struct eh_region *>::iterator I = Handlers.begin(),
2242 E = Handlers.end(); I != E; ++I) {
2243 unsigned UnwindNo = get_eh_region_number(*I);
2245 if (UnwindNo < PostPads.size())
2246 TargetBB = PostPads[UnwindNo];
2248 if (TargetBB)
2249 break;
2252 if (TargetBB) {
2253 Builder.CreateBr(TargetBB);
2254 } else {
2255 assert(can_throw_external_1(i, true) &&
2256 "Must-not-throw region handled by runtime?");
2257 // Unwinding continues in the caller.
2258 if (!UnwindBB)
2259 UnwindBB = BasicBlock::Create(Context, "Unwind");
2260 Builder.CreateBr(UnwindBB);
2263 Handlers.clear();
2267 /// EmitUnwindBlock - Emit the lazily created EH unwind block.
2268 void TreeToLLVM::EmitUnwindBlock() {
2269 if (UnwindBB) {
2270 CreateExceptionValues();
2271 EmitBlock(UnwindBB);
2272 // Fetch and store exception handler.
2273 Value *Arg = Builder.CreateLoad(ExceptionValue, "eh_ptr");
2274 assert(llvm_unwind_resume_libfunc && "no unwind resume function!");
2276 // As we're emitting a naked call (not an expression) going through
2277 // EmitCallOf would be wasteful and incorrect. Manually adjust
2278 // the calling convention for this call here if necessary.
2279 #ifdef TARGET_ADJUST_LLVM_CC
2280 tree fntype = TREE_TYPE(llvm_unwind_resume_libfunc);
2281 CallingConv::ID CallingConvention = CallingConv::C;
2283 TARGET_ADJUST_LLVM_CC(CallingConvention, fntype);
2284 CallInst *Call = Builder.CreateCall(DECL_LLVM(llvm_unwind_resume_libfunc),
2285 Arg);
2286 Call->setCallingConv(CallingConvention);
2287 #else
2288 Builder.CreateCall(DECL_LLVM(llvm_unwind_resume_libfunc), Arg);
2289 #endif
2290 Builder.CreateUnreachable();
2294 //===----------------------------------------------------------------------===//
2295 // ... Expressions ...
2296 //===----------------------------------------------------------------------===//
2298 static bool canEmitRegisterVariable(tree exp) {
2299 // Only variables can be marked as 'register'.
2300 if (TREE_CODE(exp) != VAR_DECL || !DECL_REGISTER(exp))
2301 return false;
2303 // We can emit inline assembler for access to global register variables.
2304 if (TREE_STATIC(exp) || DECL_EXTERNAL(exp) || TREE_PUBLIC(exp))
2305 return true;
2307 // Emit inline asm if this is local variable with assembler name on it.
2308 if (DECL_ASSEMBLER_NAME_SET_P(exp))
2309 return true;
2311 // Otherwise - it's normal automatic variable.
2312 return false;
2316 /// EmitLoadOfLValue - When an l-value expression is used in a context that
2317 /// requires an r-value, this method emits the lvalue computation, then loads
2318 /// the result.
2319 Value *TreeToLLVM::EmitLoadOfLValue(tree exp, const MemRef *DestLoc) {
2320 // If this is a gimple temporary, don't emit a load, just use the result.
2321 if (isGimpleTemporary(exp)) {
2322 if (DECL_LLVM_SET_P(exp))
2323 return DECL_LLVM(exp);
2324 // Since basic blocks are output in no particular order, it is perfectly
2325 // possible to encounter a use of a gimple temporary before encountering
2326 // its definition, which is what has happened here. This happens rarely
2327 // in practice, so there's no point in trying to do anything clever: just
2328 // demote to an ordinary variable and create an alloca to hold its value.
2329 DECL_GIMPLE_FORMAL_TEMP_P(exp) = 0;
2330 EmitAutomaticVariableDecl(exp);
2331 // Fall through.
2332 } else if (canEmitRegisterVariable(exp)) {
2333 // If this is a register variable, EmitLV can't handle it (there is no
2334 // l-value of a register variable). Emit an inline asm node that copies the
2335 // value out of the specified register.
2336 return EmitReadOfRegisterVariable(exp, DestLoc);
2339 LValue LV = EmitLV(exp);
2340 bool isVolatile = TREE_THIS_VOLATILE(exp);
2341 const Type *Ty = ConvertType(TREE_TYPE(exp));
2342 unsigned Alignment = LV.getAlignment();
2343 if (TREE_CODE(exp) == COMPONENT_REF)
2344 if (const StructType *STy =
2345 dyn_cast<StructType>(ConvertType(TREE_TYPE(TREE_OPERAND(exp, 0)))))
2346 if (STy->isPacked())
2347 // Packed struct members use 1 byte alignment
2348 Alignment = 1;
2351 if (!LV.isBitfield()) {
2352 if (!DestLoc) {
2353 // Scalar value: emit a load.
2354 Value *Ptr = BitCastToType(LV.Ptr, Ty->getPointerTo());
2355 LoadInst *LI = Builder.CreateLoad(Ptr, isVolatile);
2356 LI->setAlignment(Alignment);
2357 return LI;
2358 } else {
2359 EmitAggregateCopy(*DestLoc, MemRef(LV.Ptr, Alignment, isVolatile),
2360 TREE_TYPE(exp));
2361 return 0;
2363 } else {
2364 // This is a bitfield reference.
2365 if (!LV.BitSize)
2366 return Constant::getNullValue(Ty);
2368 const Type *ValTy = cast<PointerType>(LV.Ptr->getType())->getElementType();
2369 unsigned ValSizeInBits = ValTy->getPrimitiveSizeInBits();
2371 // The number of loads needed to read the entire bitfield.
2372 unsigned Strides = 1 + (LV.BitStart + LV.BitSize - 1) / ValSizeInBits;
2374 assert(ValTy->isInteger() && "Invalid bitfield lvalue!");
2375 assert(ValSizeInBits > LV.BitStart && "Bad bitfield lvalue!");
2376 assert(ValSizeInBits >= LV.BitSize && "Bad bitfield lvalue!");
2377 assert(2*ValSizeInBits > LV.BitSize+LV.BitStart && "Bad bitfield lvalue!");
2379 Value *Result = NULL;
2381 for (unsigned I = 0; I < Strides; I++) {
2382 unsigned Index = BYTES_BIG_ENDIAN ? I : Strides - I - 1; // MSB first
2383 unsigned ThisFirstBit = Index * ValSizeInBits;
2384 unsigned ThisLastBitPlusOne = ThisFirstBit + ValSizeInBits;
2385 if (ThisFirstBit < LV.BitStart)
2386 ThisFirstBit = LV.BitStart;
2387 if (ThisLastBitPlusOne > LV.BitStart+LV.BitSize)
2388 ThisLastBitPlusOne = LV.BitStart+LV.BitSize;
2390 Value *Ptr = Index ?
2391 Builder.CreateGEP(LV.Ptr,
2392 ConstantInt::get(Type::getInt32Ty(Context), Index)) :
2393 LV.Ptr;
2394 LoadInst *LI = Builder.CreateLoad(Ptr, isVolatile);
2395 LI->setAlignment(Alignment);
2396 Value *Val = LI;
2398 unsigned BitsInVal = ThisLastBitPlusOne - ThisFirstBit;
2399 unsigned FirstBitInVal = ThisFirstBit % ValSizeInBits;
2401 if (BYTES_BIG_ENDIAN)
2402 FirstBitInVal = ValSizeInBits-FirstBitInVal-BitsInVal;
2404 // Mask the bits out by shifting left first, then shifting right. The
2405 // LLVM optimizer will turn this into an AND if this is an unsigned
2406 // expression.
2408 if (FirstBitInVal+BitsInVal != ValSizeInBits) {
2409 Value *ShAmt = ConstantInt::get(ValTy, ValSizeInBits -
2410 (FirstBitInVal+BitsInVal));
2411 Val = Builder.CreateShl(Val, ShAmt);
2414 // Shift right required?
2415 if (ValSizeInBits != BitsInVal) {
2416 bool AddSignBits = !TYPE_UNSIGNED(TREE_TYPE(exp)) && !Result;
2417 Value *ShAmt = ConstantInt::get(ValTy, ValSizeInBits-BitsInVal);
2418 Val = AddSignBits ?
2419 Builder.CreateAShr(Val, ShAmt) : Builder.CreateLShr(Val, ShAmt);
2422 if (Result) {
2423 Value *ShAmt = ConstantInt::get(ValTy, BitsInVal);
2424 Result = Builder.CreateShl(Result, ShAmt);
2425 Result = Builder.CreateOr(Result, Val);
2426 } else {
2427 Result = Val;
2431 if (TYPE_UNSIGNED(TREE_TYPE(exp)))
2432 return CastToUIntType(Result, Ty);
2433 else
2434 return CastToSIntType(Result, Ty);
2438 Value *TreeToLLVM::EmitADDR_EXPR(tree exp) {
2439 LValue LV = EmitLV(TREE_OPERAND(exp, 0));
2440 assert((!LV.isBitfield() || LV.BitStart == 0) &&
2441 "It is illegal to take the address of a bitfield");
2442 // Perform a cast here if necessary. For example, GCC sometimes forms an
2443 // ADDR_EXPR where the operand is an array, and the ADDR_EXPR type is a
2444 // pointer to the first element.
2445 return BitCastToType(LV.Ptr, ConvertType(TREE_TYPE(exp)));
2448 Value *TreeToLLVM::EmitOBJ_TYPE_REF(tree exp) {
2449 return BitCastToType(Emit(OBJ_TYPE_REF_EXPR(exp), 0),
2450 ConvertType(TREE_TYPE(exp)));
2453 Value *TreeToLLVM::EmitCALL_EXPR(tree exp, const MemRef *DestLoc) {
2454 // Check for a built-in function call. If we can lower it directly, do so
2455 // now.
2456 tree fndecl = get_callee_fndecl(exp);
2457 if (fndecl && DECL_BUILT_IN(fndecl) &&
2458 DECL_BUILT_IN_CLASS(fndecl) != BUILT_IN_FRONTEND) {
2459 Value *Res = 0;
2460 if (EmitBuiltinCall(exp, fndecl, DestLoc, Res))
2461 return Res;
2464 Value *Callee = Emit(TREE_OPERAND(exp, 0), 0);
2466 assert(TREE_TYPE (TREE_OPERAND (exp, 0)) &&
2467 (TREE_CODE(TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE ||
2468 TREE_CODE(TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE ||
2469 TREE_CODE(TREE_TYPE (TREE_OPERAND (exp, 0))) == BLOCK_POINTER_TYPE)
2470 && "Not calling a function pointer?");
2471 tree function_type = TREE_TYPE(TREE_TYPE (TREE_OPERAND (exp, 0)));
2472 CallingConv::ID CallingConv;
2473 AttrListPtr PAL;
2475 const Type *Ty = TheTypeConverter->ConvertFunctionType(function_type,
2476 fndecl,
2477 TREE_OPERAND(exp, 2),
2478 CallingConv, PAL);
2480 // If this is a direct call to a function using a static chain then we need
2481 // to ensure the function type is the one just calculated: it has an extra
2482 // parameter for the chain.
2483 Callee = BitCastToType(Callee, Ty->getPointerTo());
2485 // EmitCall(exp, DestLoc);
2486 Value *Result = EmitCallOf(Callee, exp, DestLoc, PAL);
2488 // If the function has the volatile bit set, then it is a "noreturn" function.
2489 // Output an unreachable instruction right after the function to prevent LLVM
2490 // from thinking that control flow will fall into the subsequent block.
2492 if (fndecl && TREE_THIS_VOLATILE(fndecl)) {
2493 Builder.CreateUnreachable();
2494 EmitBlock(BasicBlock::Create(Context, ""));
2496 return Result;
2499 /// llvm_load_scalar_argument - Load value located at LOC.
2500 static Value *llvm_load_scalar_argument(Value *L,
2501 const llvm::Type *LLVMTy,
2502 unsigned RealSize,
2503 LLVMBuilder &Builder) {
2504 if (!RealSize)
2505 return UndefValue::get(LLVMTy);
2507 // Not clear what this is supposed to do on big endian machines...
2508 assert(!BYTES_BIG_ENDIAN && "Unsupported case - please report");
2509 assert(isa<IntegerType>(LLVMTy) && "Expected an integer value!");
2510 const Type *LoadType = IntegerType::get(Context, RealSize * 8);
2511 L = Builder.CreateBitCast(L, LoadType->getPointerTo());
2512 Value *Val = Builder.CreateLoad(L);
2513 if (LoadType->getPrimitiveSizeInBits() >= LLVMTy->getPrimitiveSizeInBits())
2514 Val = Builder.CreateTrunc(Val, LLVMTy);
2515 else
2516 Val = Builder.CreateZExt(Val, LLVMTy);
2517 return Val;
2520 #ifndef LLVM_LOAD_SCALAR_ARGUMENT
2521 #define LLVM_LOAD_SCALAR_ARGUMENT(LOC,TY,SIZE,BUILDER) \
2522 llvm_load_scalar_argument((LOC),(TY),(SIZE),(BUILDER))
2523 #endif
2525 namespace {
2526 /// FunctionCallArgumentConversion - This helper class is driven by the ABI
2527 /// definition for this target to figure out how to pass arguments into the
2528 /// stack/regs for a function call.
2529 struct FunctionCallArgumentConversion : public DefaultABIClient {
2530 SmallVector<Value*, 16> &CallOperands;
2531 SmallVector<Value*, 2> LocStack;
2532 const FunctionType *FTy;
2533 const MemRef *DestLoc;
2534 bool useReturnSlot;
2535 LLVMBuilder &Builder;
2536 Value *TheValue;
2537 MemRef RetBuf;
2538 CallingConv::ID &CallingConv;
2539 bool isShadowRet;
2540 bool isAggrRet;
2541 unsigned Offset;
2543 FunctionCallArgumentConversion(SmallVector<Value*, 16> &ops,
2544 const FunctionType *FnTy,
2545 const MemRef *destloc,
2546 bool ReturnSlotOpt,
2547 LLVMBuilder &b,
2548 CallingConv::ID &CC)
2549 : CallOperands(ops), FTy(FnTy), DestLoc(destloc),
2550 useReturnSlot(ReturnSlotOpt), Builder(b), CallingConv(CC),
2551 isShadowRet(false), isAggrRet(false), Offset(0) { }
2553 /// getCallingConv - This provides the desired CallingConv for the function.
2554 CallingConv::ID& getCallingConv(void) { return CallingConv; }
2556 // Push the address of an argument.
2557 void pushAddress(Value *Loc) {
2558 assert(Loc && "Invalid location!");
2559 LocStack.push_back(Loc);
2562 // Push the value of an argument.
2563 void pushValue(Value *V) {
2564 assert(LocStack.empty() && "Value only allowed at top level!");
2565 LocStack.push_back(NULL);
2566 TheValue = V;
2569 // Get the address of the current location.
2570 Value *getAddress(void) {
2571 assert(!LocStack.empty());
2572 Value *&Loc = LocStack.back();
2573 if (!Loc) {
2574 // A value. Store to a temporary, and return the temporary's address.
2575 // Any future access to this argument will reuse the same address.
2576 Loc = TheTreeToLLVM->CreateTemporary(TheValue->getType());
2577 Builder.CreateStore(TheValue, Loc);
2579 return Loc;
2582 // Get the value of the current location (of type Ty).
2583 Value *getValue(const Type *Ty) {
2584 assert(!LocStack.empty());
2585 Value *Loc = LocStack.back();
2586 if (Loc) {
2587 // An address. Convert to the right type and load the value out.
2588 Loc = Builder.CreateBitCast(Loc, Ty->getPointerTo());
2589 return Builder.CreateLoad(Loc, "val");
2590 } else {
2591 // A value - just return it.
2592 assert(TheValue->getType() == Ty && "Value not of expected type!");
2593 return TheValue;
2597 void clear() {
2598 assert(LocStack.size() == 1 && "Imbalance!");
2599 LocStack.clear();
2602 bool isShadowReturn() const { return isShadowRet; }
2603 bool isAggrReturn() { return isAggrRet; }
2605 // EmitShadowResult - If the return result was redirected to a buffer,
2606 // emit it now.
2607 Value *EmitShadowResult(tree type, const MemRef *DestLoc) {
2608 if (!RetBuf.Ptr)
2609 return 0;
2611 if (DestLoc) {
2612 // Copy out the aggregate return value now.
2613 assert(ConvertType(type) ==
2614 cast<PointerType>(RetBuf.Ptr->getType())->getElementType() &&
2615 "Inconsistent result types!");
2616 TheTreeToLLVM->EmitAggregateCopy(*DestLoc, RetBuf, type);
2617 return 0;
2618 } else {
2619 // Read out the scalar return value now.
2620 return Builder.CreateLoad(RetBuf.Ptr, "result");
2624 /// HandleScalarResult - This callback is invoked if the function returns a
2625 /// simple scalar result value.
2626 void HandleScalarResult(const Type *RetTy) {
2627 // There is nothing to do here if we return a scalar or void.
2628 assert(DestLoc == 0 &&
2629 "Call returns a scalar but caller expects aggregate!");
2632 /// HandleAggregateResultAsScalar - This callback is invoked if the function
2633 /// returns an aggregate value by bit converting it to the specified scalar
2634 /// type and returning that.
2635 void HandleAggregateResultAsScalar(const Type *ScalarTy,
2636 unsigned Offset = 0) {
2637 this->Offset = Offset;
2640 /// HandleAggregateResultAsAggregate - This callback is invoked if the
2641 /// function returns an aggregate value using multiple return values.
2642 void HandleAggregateResultAsAggregate(const Type *AggrTy) {
2643 // There is nothing to do here.
2644 isAggrRet = true;
2647 /// HandleAggregateShadowResult - This callback is invoked if the function
2648 /// returns an aggregate value by using a "shadow" first parameter. If
2649 /// RetPtr is set to true, the pointer argument itself is returned from the
2650 /// function.
2651 void HandleAggregateShadowResult(const PointerType *PtrArgTy,
2652 bool RetPtr) {
2653 // We need to pass memory to write the return value into.
2654 // FIXME: alignment and volatility are being ignored!
2655 assert(!DestLoc || PtrArgTy == DestLoc->Ptr->getType());
2657 if (DestLoc == 0) {
2658 // The result is unused, but still needs to be stored somewhere.
2659 Value *Buf = TheTreeToLLVM->CreateTemporary(PtrArgTy->getElementType());
2660 CallOperands.push_back(Buf);
2661 } else if (useReturnSlot) {
2662 // Letting the call write directly to the final destination is safe and
2663 // may be required. Do not use a buffer.
2664 CallOperands.push_back(DestLoc->Ptr);
2665 } else {
2666 // Letting the call write directly to the final destination may not be
2667 // safe (eg: if DestLoc aliases a parameter) and is not required - pass
2668 // a buffer and copy it to DestLoc after the call.
2669 RetBuf = TheTreeToLLVM->CreateTempLoc(PtrArgTy->getElementType());
2670 CallOperands.push_back(RetBuf.Ptr);
2673 // Note the use of a shadow argument.
2674 isShadowRet = true;
2677 /// HandleScalarShadowResult - This callback is invoked if the function
2678 /// returns a scalar value by using a "shadow" first parameter, which is a
2679 /// pointer to the scalar, of type PtrArgTy. If RetPtr is set to true,
2680 /// the pointer argument itself is returned from the function.
2681 void HandleScalarShadowResult(const PointerType *PtrArgTy, bool RetPtr) {
2682 assert(DestLoc == 0 &&
2683 "Call returns a scalar but caller expects aggregate!");
2684 // Create a buffer to hold the result. The result will be loaded out of
2685 // it after the call.
2686 RetBuf = TheTreeToLLVM->CreateTempLoc(PtrArgTy->getElementType());
2687 CallOperands.push_back(RetBuf.Ptr);
2689 // Note the use of a shadow argument.
2690 isShadowRet = true;
2693 /// HandleScalarArgument - This is the primary callback that specifies an
2694 /// LLVM argument to pass. It is only used for first class types.
2695 void HandleScalarArgument(const llvm::Type *LLVMTy, tree type,
2696 unsigned RealSize = 0) {
2697 Value *Loc = NULL;
2698 if (RealSize) {
2699 Value *L = getAddress();
2700 Loc = LLVM_LOAD_SCALAR_ARGUMENT(L,LLVMTy,RealSize,Builder);
2701 } else
2702 Loc = getValue(LLVMTy);
2704 // Perform any implicit type conversions.
2705 if (CallOperands.size() < FTy->getNumParams()) {
2706 const Type *CalledTy= FTy->getParamType(CallOperands.size());
2707 if (Loc->getType() != CalledTy) {
2708 assert(type && "Inconsistent parameter types?");
2709 bool isSigned = !TYPE_UNSIGNED(type);
2710 Loc = TheTreeToLLVM->CastToAnyType(Loc, isSigned, CalledTy, false);
2714 CallOperands.push_back(Loc);
2717 /// HandleByInvisibleReferenceArgument - This callback is invoked if a
2718 /// pointer (of type PtrTy) to the argument is passed rather than the
2719 /// argument itself.
2720 void HandleByInvisibleReferenceArgument(const llvm::Type *PtrTy, tree type){
2721 Value *Loc = getAddress();
2722 Loc = Builder.CreateBitCast(Loc, PtrTy);
2723 CallOperands.push_back(Loc);
2726 /// HandleByValArgument - This callback is invoked if the aggregate function
2727 /// argument is passed by value. It is lowered to a parameter passed by
2728 /// reference with an additional parameter attribute "ByVal".
2729 void HandleByValArgument(const llvm::Type *LLVMTy, tree type) {
2730 Value *Loc = getAddress();
2731 assert(LLVMTy->getPointerTo() == Loc->getType());
2732 CallOperands.push_back(Loc);
2735 /// HandleFCAArgument - This callback is invoked if the aggregate function
2736 /// argument is passed as a first class aggregate.
2737 void HandleFCAArgument(const llvm::Type *LLVMTy,
2738 tree type ATTRIBUTE_UNUSED) {
2739 Value *Loc = getAddress();
2740 assert(LLVMTy->getPointerTo() == Loc->getType());
2741 CallOperands.push_back(Builder.CreateLoad(Loc));
2744 /// EnterField - Called when we're about the enter the field of a struct
2745 /// or union. FieldNo is the number of the element we are entering in the
2746 /// LLVM Struct, StructTy is the LLVM type of the struct we are entering.
2747 void EnterField(unsigned FieldNo, const llvm::Type *StructTy) {
2748 Value *Loc = getAddress();
2749 Loc = Builder.CreateBitCast(Loc, StructTy->getPointerTo());
2750 pushAddress(Builder.CreateStructGEP(Loc, FieldNo, "elt"));
2752 void ExitField() {
2753 assert(!LocStack.empty());
2754 LocStack.pop_back();
2759 /// EmitCallOf - Emit a call to the specified callee with the operands specified
2760 /// in the CALL_EXP 'exp'. If the result of the call is a scalar, return the
2761 /// result, otherwise store it in DestLoc.
2762 Value *TreeToLLVM::EmitCallOf(Value *Callee, tree exp, const MemRef *DestLoc,
2763 const AttrListPtr &InPAL) {
2764 BasicBlock *LandingPad = 0; // Non-zero indicates an invoke.
2766 AttrListPtr PAL = InPAL;
2767 if (PAL.isEmpty() && isa<Function>(Callee))
2768 PAL = cast<Function>(Callee)->getAttributes();
2770 // Work out whether to use an invoke or an ordinary call.
2771 if (!tree_could_throw_p(exp))
2772 // This call does not throw - mark it 'nounwind'.
2773 PAL = PAL.addAttr(~0, Attribute::NoUnwind);
2775 if (!PAL.paramHasAttr(~0, Attribute::NoUnwind)) {
2776 // This call may throw. Determine if we need to generate
2777 // an invoke rather than a simple call.
2778 int RegionNo = lookup_stmt_eh_region(exp);
2780 // Is the call contained in an exception handling region?
2781 if (RegionNo > 0) {
2782 // Are there any exception handlers for this region?
2783 if (can_throw_internal_1(RegionNo, false)) {
2784 // There are - turn the call into an invoke.
2785 LandingPads.grow(RegionNo);
2786 BasicBlock *&ThisPad = LandingPads[RegionNo];
2788 // Create a landing pad if one didn't exist already.
2789 if (!ThisPad)
2790 ThisPad = BasicBlock::Create(Context, "lpad");
2792 LandingPad = ThisPad;
2793 } else {
2794 assert(can_throw_external_1(RegionNo, false) &&
2795 "Must-not-throw region handled by runtime?");
2800 tree fndecl = get_callee_fndecl(exp);
2801 tree fntype = fndecl ?
2802 TREE_TYPE(fndecl) : TREE_TYPE (TREE_TYPE(TREE_OPERAND (exp, 0)));
2804 // Determine the calling convention.
2805 CallingConv::ID CallingConvention = CallingConv::C;
2806 #ifdef TARGET_ADJUST_LLVM_CC
2807 TARGET_ADJUST_LLVM_CC(CallingConvention, fntype);
2808 #endif
2810 SmallVector<Value*, 16> CallOperands;
2811 const PointerType *PFTy = cast<PointerType>(Callee->getType());
2812 const FunctionType *FTy = cast<FunctionType>(PFTy->getElementType());
2813 FunctionCallArgumentConversion Client(CallOperands, FTy, DestLoc,
2814 CALL_EXPR_RETURN_SLOT_OPT(exp),
2815 Builder, CallingConvention);
2816 TheLLVMABI ABIConverter(Client);
2818 // Handle the result, including struct returns.
2819 ABIConverter.HandleReturnType(TREE_TYPE(exp),
2820 fndecl ? fndecl : exp,
2821 fndecl ? DECL_BUILT_IN(fndecl) : false);
2823 // Pass the static chain, if any, as the first parameter.
2824 if (TREE_OPERAND(exp, 2))
2825 CallOperands.push_back(Emit(TREE_OPERAND(exp, 2), 0));
2827 // Loop over the arguments, expanding them and adding them to the op list.
2828 std::vector<const Type*> ScalarArgs;
2829 for (tree arg = TREE_OPERAND(exp, 1); arg; arg = TREE_CHAIN(arg)) {
2830 tree type = TREE_TYPE(TREE_VALUE(arg));
2831 const Type *ArgTy = ConvertType(type);
2833 // Push the argument.
2834 if (ArgTy->isSingleValueType()) {
2835 // A scalar - push the value.
2836 Client.pushValue(Emit(TREE_VALUE(arg), 0));
2837 } else if (LLVM_SHOULD_PASS_AGGREGATE_AS_FCA(type, ArgTy)) {
2838 // A first class aggregate - push the value.
2839 LValue ArgVal = EmitLV(TREE_VALUE(arg));
2840 Client.pushValue(Builder.CreateLoad(ArgVal.Ptr));
2841 } else {
2842 // An aggregate - push the address.
2843 LValue ArgVal = EmitLV(TREE_VALUE(arg));
2844 assert(!ArgVal.isBitfield() && "Bitfields are first-class types!");
2845 Client.pushAddress(ArgVal.Ptr);
2848 Attributes Attrs = Attribute::None;
2850 unsigned OldSize = CallOperands.size();
2852 ABIConverter.HandleArgument(type, ScalarArgs, &Attrs);
2854 if (Attrs != Attribute::None) {
2855 // If the argument is split into multiple scalars, assign the
2856 // attributes to all scalars of the aggregate.
2857 for (unsigned i = OldSize + 1; i <= CallOperands.size(); ++i) {
2858 PAL = PAL.addAttr(i, Attrs);
2862 Client.clear();
2865 // Compile stuff like:
2866 // %tmp = call float (...)* bitcast (float ()* @foo to float (...)*)( )
2867 // to:
2868 // %tmp = call float @foo( )
2869 // This commonly occurs due to C "implicit ..." semantics.
2870 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Callee)) {
2871 if (CallOperands.empty() && CE->getOpcode() == Instruction::BitCast) {
2872 Constant *RealCallee = CE->getOperand(0);
2873 assert(isa<PointerType>(RealCallee->getType()) &&
2874 "Bitcast to ptr not from ptr?");
2875 const PointerType *RealPT = cast<PointerType>(RealCallee->getType());
2876 if (const FunctionType *RealFT =
2877 dyn_cast<FunctionType>(RealPT->getElementType())) {
2878 const PointerType *ActualPT = cast<PointerType>(Callee->getType());
2879 const FunctionType *ActualFT =
2880 cast<FunctionType>(ActualPT->getElementType());
2881 if (RealFT->getReturnType() == ActualFT->getReturnType() &&
2882 RealFT->getNumParams() == 0)
2883 Callee = RealCallee;
2888 Value *Call;
2889 if (!LandingPad) {
2890 Call = Builder.CreateCall(Callee, CallOperands.begin(), CallOperands.end());
2891 cast<CallInst>(Call)->setCallingConv(CallingConvention);
2892 cast<CallInst>(Call)->setAttributes(PAL);
2893 } else {
2894 BasicBlock *NextBlock = BasicBlock::Create(Context, "invcont");
2895 Call = Builder.CreateInvoke(Callee, NextBlock, LandingPad,
2896 CallOperands.begin(), CallOperands.end());
2897 cast<InvokeInst>(Call)->setCallingConv(CallingConvention);
2898 cast<InvokeInst>(Call)->setAttributes(PAL);
2899 EmitBlock(NextBlock);
2902 if (Client.isShadowReturn())
2903 return Client.EmitShadowResult(TREE_TYPE(exp), DestLoc);
2905 if (Call->getType()->isVoidTy())
2906 return 0;
2908 if (Client.isAggrReturn()) {
2909 if (TD.getTypeAllocSize(Call->getType()) <=
2910 TD.getTypeAllocSize(cast<PointerType>(DestLoc->Ptr->getType())
2911 ->getElementType())) {
2912 Value *Dest = BitCastToType(DestLoc->Ptr, Call->getType()->getPointerTo());
2913 LLVM_EXTRACT_MULTIPLE_RETURN_VALUE(Call,Dest,DestLoc->Volatile,Builder);
2914 } else {
2915 // The call will return an aggregate value in registers, but
2916 // those registers are bigger than DestLoc. Allocate a
2917 // temporary to match the registers, store the registers there,
2918 // cast the temporary into the correct (smaller) type, and using
2919 // the correct type, copy the value into DestLoc. Assume the
2920 // optimizer will delete the temporary and clean this up.
2921 AllocaInst *biggerTmp = CreateTemporary(Call->getType());
2922 LLVM_EXTRACT_MULTIPLE_RETURN_VALUE(Call,biggerTmp,/*Volatile=*/false,
2923 Builder);
2924 EmitAggregateCopy(*DestLoc,
2925 MemRef(BitCastToType(biggerTmp,Call->getType()->
2926 getPointerTo()),
2927 DestLoc->getAlignment(),
2928 DestLoc->Volatile),
2929 TREE_TYPE(exp));
2931 return 0;
2934 // If the caller expects an aggregate, we have a situation where the ABI for
2935 // the current target specifies that the aggregate be returned in scalar
2936 // registers even though it is an aggregate. We must bitconvert the scalar
2937 // to the destination aggregate type. We do this by casting the DestLoc
2938 // pointer and storing into it. The store does not necessarily start at the
2939 // beginning of the aggregate (x86-64).
2940 if (!DestLoc)
2941 return Call; // Normal scalar return.
2943 Value *Ptr = DestLoc->Ptr;
2944 if (Client.Offset) {
2945 Ptr = BitCastToType(Ptr, Type::getInt8PtrTy(Context));
2946 Ptr = Builder.CreateGEP(Ptr,
2947 ConstantInt::get(TD.getIntPtrType(Context), Client.Offset));
2949 Ptr = BitCastToType(Ptr, Call->getType()->getPointerTo());
2950 StoreInst *St = Builder.CreateStore(Call, Ptr, DestLoc->Volatile);
2951 St->setAlignment(DestLoc->getAlignment());
2952 return 0;
2955 /// HandleMultiplyDefinedGimpleTemporary - Gimple temporaries *mostly* have a
2956 /// single definition, in which case all uses are dominated by the definition.
2957 /// This routine exists to handle the rare case of a gimple temporary with
2958 /// multiple definitions. It turns the temporary into an ordinary automatic
2959 /// variable by creating an alloca for it, initializing the alloca with the
2960 /// first definition that was seen, and fixing up any existing uses to load
2961 /// the alloca instead.
2963 void TreeToLLVM::HandleMultiplyDefinedGimpleTemporary(tree Var) {
2964 Value *UniqVal = DECL_LLVM(Var);
2965 assert(isa<CastInst>(UniqVal) && "Invalid value for gimple temporary!");
2966 Value *FirstVal = cast<CastInst>(UniqVal)->getOperand(0);
2968 // Create a new temporary and set the VAR_DECL to use it as the llvm location.
2969 Value *NewTmp = CreateTemporary(FirstVal->getType());
2970 SET_DECL_LLVM(Var, NewTmp);
2972 // Store the already existing initial value into the alloca. If the value
2973 // being stored is an instruction, emit the store right after the instruction,
2974 // otherwise, emit it into the entry block.
2975 StoreInst *SI = new StoreInst(FirstVal, NewTmp);
2977 BasicBlock::iterator InsertPt;
2978 if (Instruction *I = dyn_cast<Instruction>(FirstVal)) {
2979 InsertPt = I; // Insert after the init instruction.
2981 bool InsertPtFinal = false;
2982 // If the instruction is an alloca in the entry block, the insert point
2983 // will be before the alloca. Advance to the AllocaInsertionPoint if we are
2984 // before it.
2985 if (I->getParent() == &Fn->front()) {
2986 for (BasicBlock::iterator CI = InsertPt, E = Fn->begin()->end();
2987 CI != E; ++CI) {
2988 if (&*CI == AllocaInsertionPoint) {
2989 InsertPt = AllocaInsertionPoint;
2990 ++InsertPt;
2991 InsertPtFinal = true; // This is the spot; stop searching.
2992 break;
2997 if (!InsertPtFinal) {
2998 // If the instruction is an invoke, the init is inserted on the normal edge.
2999 if (InvokeInst *II = dyn_cast<InvokeInst>(InsertPt)) {
3000 InsertPt = II->getNormalDest()->begin();
3001 while (isa<PHINode>(InsertPt))
3002 ++InsertPt;
3004 else
3005 ++InsertPt; // Insert after the init instruction.
3007 } else {
3008 InsertPt = AllocaInsertionPoint; // Insert after the allocas.
3009 ++InsertPt;
3011 BasicBlock *BB = InsertPt->getParent();
3012 BB->getInstList().insert(InsertPt, SI);
3014 // Replace any uses of the original value with a load of the alloca.
3015 for (Value::use_iterator U = UniqVal->use_begin(), E = UniqVal->use_end();
3016 U != E; ++U)
3017 U.getUse().set(new LoadInst(NewTmp, "mtmp", cast<Instruction>(*U)));
3019 // Finally, This is no longer a GCC temporary.
3020 DECL_GIMPLE_FORMAL_TEMP_P(Var) = 0;
3023 /// EmitMODIFY_EXPR - Note that MODIFY_EXPRs are rvalues only!
3024 /// We also handle INIT_EXPRs here; these are built by the C++ FE on rare
3025 /// occasions, and have slightly different semantics that don't affect us here.
3027 Value *TreeToLLVM::EmitMODIFY_EXPR(tree exp, const MemRef *DestLoc) {
3028 tree lhs = TREE_OPERAND (exp, 0);
3029 tree rhs = TREE_OPERAND (exp, 1);
3031 // If this is the definition of a gimple temporary, set its DECL_LLVM to the
3032 // RHS.
3033 bool LHSSigned = !TYPE_UNSIGNED(TREE_TYPE(lhs));
3034 bool RHSSigned = !TYPE_UNSIGNED(TREE_TYPE(rhs));
3035 if (isGimpleTemporary(lhs)) {
3036 // If DECL_LLVM is already set, this is a multiply defined gimple temporary.
3037 if (DECL_LLVM_SET_P(lhs)) {
3038 HandleMultiplyDefinedGimpleTemporary(lhs);
3039 return EmitMODIFY_EXPR(exp, DestLoc);
3041 Value *RHS = Emit(rhs, 0);
3042 const Type *LHSTy = ConvertType(TREE_TYPE(lhs));
3043 // The value may need to be replaced later if this temporary is multiply
3044 // defined - ensure it can be uniquely identified by not folding the cast.
3045 Instruction::CastOps opc = CastInst::getCastOpcode(RHS, RHSSigned,
3046 LHSTy, LHSSigned);
3047 CastInst *Cast = CastInst::Create(opc, RHS, LHSTy, RHS->getName());
3048 if (opc == Instruction::BitCast && RHS->getType() == LHSTy)
3049 // Simplify this no-op bitcast once the function is emitted.
3050 UniquedValues.push_back(cast<BitCastInst>(Cast));
3051 Builder.Insert(Cast);
3052 SET_DECL_LLVM(lhs, Cast);
3053 return Cast;
3054 } else if (canEmitRegisterVariable(lhs)) {
3055 // If this is a store to a register variable, EmitLV can't handle the dest
3056 // (there is no l-value of a register variable). Emit an inline asm node
3057 // that copies the value into the specified register.
3058 Value *RHS = Emit(rhs, 0);
3059 RHS = CastToAnyType(RHS, RHSSigned, ConvertType(TREE_TYPE(lhs)), LHSSigned);
3060 EmitModifyOfRegisterVariable(lhs, RHS);
3061 return RHS;
3064 LValue LV = EmitLV(lhs);
3065 bool isVolatile = TREE_THIS_VOLATILE(lhs);
3066 unsigned Alignment = LV.getAlignment();
3067 if (TREE_CODE(lhs) == COMPONENT_REF)
3068 if (const StructType *STy =
3069 dyn_cast<StructType>(ConvertType(TREE_TYPE(TREE_OPERAND(lhs, 0)))))
3070 if (STy->isPacked())
3071 // Packed struct members use 1 byte alignment
3072 Alignment = 1;
3074 if (!LV.isBitfield()) {
3075 const Type *ValTy = ConvertType(TREE_TYPE(rhs));
3076 if (ValTy->isSingleValueType()) {
3077 // Non-bitfield, scalar value. Just emit a store.
3078 Value *RHS = Emit(rhs, 0);
3079 // Convert RHS to the right type if we can. If LHS is bigger than RHS
3080 // we must convert; all the bits of LHS must be stored into. Otherwise
3081 // convert the pointer.
3082 const PointerType *PT = cast<PointerType>(LV.Ptr->getType());
3083 if (PT->getElementType()->canLosslesslyBitCastTo(RHS->getType()) ||
3084 (PT->getElementType()->getPrimitiveSizeInBits() >
3085 RHS->getType()->getPrimitiveSizeInBits()))
3086 RHS = CastToAnyType(RHS, RHSSigned, PT->getElementType(), LHSSigned);
3087 else
3088 LV.Ptr = BitCastToType(LV.Ptr, RHS->getType()->getPointerTo());
3089 StoreInst *SI = Builder.CreateStore(RHS, LV.Ptr, isVolatile);
3090 SI->setAlignment(Alignment);
3091 return RHS;
3094 // Non-bitfield aggregate value.
3095 MemRef NewLoc(LV.Ptr, Alignment, isVolatile);
3096 Emit(rhs, &NewLoc);
3098 if (DestLoc)
3099 EmitAggregateCopy(*DestLoc, NewLoc, TREE_TYPE(exp));
3101 return 0;
3104 // Last case, this is a store to a bitfield, so we have to emit a
3105 // read/modify/write sequence.
3107 Value *RHS = Emit(rhs, 0);
3109 if (!LV.BitSize)
3110 return RHS;
3112 const Type *ValTy = cast<PointerType>(LV.Ptr->getType())->getElementType();
3113 unsigned ValSizeInBits = ValTy->getPrimitiveSizeInBits();
3115 // The number of stores needed to write the entire bitfield.
3116 unsigned Strides = 1 + (LV.BitStart + LV.BitSize - 1) / ValSizeInBits;
3118 assert(ValTy->isInteger() && "Invalid bitfield lvalue!");
3119 assert(ValSizeInBits > LV.BitStart && "Bad bitfield lvalue!");
3120 assert(ValSizeInBits >= LV.BitSize && "Bad bitfield lvalue!");
3121 assert(2*ValSizeInBits > LV.BitSize+LV.BitStart && "Bad bitfield lvalue!");
3123 Value *BitSource = CastToAnyType(RHS, RHSSigned, ValTy, LHSSigned);
3125 for (unsigned I = 0; I < Strides; I++) {
3126 unsigned Index = BYTES_BIG_ENDIAN ? Strides - I - 1 : I; // LSB first
3127 unsigned ThisFirstBit = Index * ValSizeInBits;
3128 unsigned ThisLastBitPlusOne = ThisFirstBit + ValSizeInBits;
3129 if (ThisFirstBit < LV.BitStart)
3130 ThisFirstBit = LV.BitStart;
3131 if (ThisLastBitPlusOne > LV.BitStart+LV.BitSize)
3132 ThisLastBitPlusOne = LV.BitStart+LV.BitSize;
3134 Value *Ptr = Index ?
3135 Builder.CreateGEP(LV.Ptr, ConstantInt::get(Type::getInt32Ty(Context), Index)) :
3136 LV.Ptr;
3137 LoadInst *LI = Builder.CreateLoad(Ptr, isVolatile);
3138 LI->setAlignment(Alignment);
3139 Value *OldVal = LI;
3140 Value *NewVal = BitSource;
3142 unsigned BitsInVal = ThisLastBitPlusOne - ThisFirstBit;
3143 unsigned FirstBitInVal = ThisFirstBit % ValSizeInBits;
3145 if (BYTES_BIG_ENDIAN)
3146 FirstBitInVal = ValSizeInBits-FirstBitInVal-BitsInVal;
3148 // If not storing into the zero'th bit, shift the Src value to the left.
3149 if (FirstBitInVal) {
3150 Value *ShAmt = ConstantInt::get(ValTy, FirstBitInVal);
3151 NewVal = Builder.CreateShl(NewVal, ShAmt);
3154 // Next, if this doesn't touch the top bit, mask out any bits that shouldn't
3155 // be set in the result.
3156 uint64_t MaskVal = ((1ULL << BitsInVal)-1) << FirstBitInVal;
3157 Constant *Mask = ConstantInt::get(Type::getInt64Ty(Context), MaskVal);
3158 Mask = Builder.getFolder().CreateTruncOrBitCast(Mask, ValTy);
3160 if (FirstBitInVal+BitsInVal != ValSizeInBits)
3161 NewVal = Builder.CreateAnd(NewVal, Mask);
3163 // Next, mask out the bits this bit-field should include from the old value.
3164 Mask = Builder.getFolder().CreateNot(Mask);
3165 OldVal = Builder.CreateAnd(OldVal, Mask);
3167 // Finally, merge the two together and store it.
3168 NewVal = Builder.CreateOr(OldVal, NewVal);
3170 StoreInst *SI = Builder.CreateStore(NewVal, Ptr, isVolatile);
3171 SI->setAlignment(Alignment);
3173 if (I + 1 < Strides) {
3174 Value *ShAmt = ConstantInt::get(ValTy, BitsInVal);
3175 BitSource = Builder.CreateLShr(BitSource, ShAmt);
3179 return RHS;
3182 Value *TreeToLLVM::EmitNOP_EXPR(tree exp, const MemRef *DestLoc) {
3183 if (TREE_CODE(TREE_TYPE(exp)) == VOID_TYPE && // deleted statement.
3184 TREE_CODE(TREE_OPERAND(exp, 0)) == INTEGER_CST)
3185 return 0;
3186 tree Op = TREE_OPERAND(exp, 0);
3187 const Type *Ty = ConvertType(TREE_TYPE(exp));
3188 bool OpIsSigned = !TYPE_UNSIGNED(TREE_TYPE(Op));
3189 bool ExpIsSigned = !TYPE_UNSIGNED(TREE_TYPE(exp));
3190 if (DestLoc == 0) {
3191 // Scalar to scalar copy.
3192 assert(!isAggregateTreeType(TREE_TYPE(Op))
3193 && "Aggregate to scalar nop_expr!");
3194 Value *OpVal = Emit(Op, DestLoc);
3195 if (Ty->isVoidTy()) return 0;
3196 return CastToAnyType(OpVal, OpIsSigned, Ty, ExpIsSigned);
3197 } else if (isAggregateTreeType(TREE_TYPE(Op))) {
3198 // Aggregate to aggregate copy.
3199 MemRef NewLoc = *DestLoc;
3200 NewLoc.Ptr = BitCastToType(DestLoc->Ptr, Ty->getPointerTo());
3201 Value *OpVal = Emit(Op, &NewLoc);
3202 assert(OpVal == 0 && "Shouldn't cast scalar to aggregate!");
3203 return 0;
3206 // Scalar to aggregate copy.
3207 Value *OpVal = Emit(Op, 0);
3208 Value *Ptr = BitCastToType(DestLoc->Ptr, OpVal->getType()->getPointerTo());
3209 StoreInst *St = Builder.CreateStore(OpVal, Ptr, DestLoc->Volatile);
3210 St->setAlignment(DestLoc->getAlignment());
3211 return 0;
3214 Value *TreeToLLVM::EmitCONVERT_EXPR(tree exp, const MemRef *DestLoc) {
3215 assert(!DestLoc && "Cannot handle aggregate casts!");
3216 Value *Op = Emit(TREE_OPERAND(exp, 0), 0);
3217 bool OpIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0)));
3218 bool ExpIsSigned = !TYPE_UNSIGNED(TREE_TYPE(exp));
3219 return CastToAnyType(Op, OpIsSigned, ConvertType(TREE_TYPE(exp)),ExpIsSigned);
3222 Value *TreeToLLVM::EmitVIEW_CONVERT_EXPR(tree exp, const MemRef *DestLoc) {
3223 tree Op = TREE_OPERAND(exp, 0);
3225 if (isAggregateTreeType(TREE_TYPE(Op))) {
3226 MemRef Target;
3227 if (DestLoc)
3228 // This is an aggregate-to-agg VIEW_CONVERT_EXPR, just evaluate in place.
3229 Target = *DestLoc;
3230 else
3231 // This is an aggregate-to-scalar VIEW_CONVERT_EXPR, evaluate, then load.
3232 Target = CreateTempLoc(ConvertType(TREE_TYPE(exp)));
3234 // Make the destination look like the source type.
3235 const Type *OpTy = ConvertType(TREE_TYPE(Op));
3236 Target.Ptr = BitCastToType(Target.Ptr, OpTy->getPointerTo());
3238 // Needs to be in sync with EmitLV.
3239 switch (TREE_CODE(Op)) {
3240 default: {
3241 Value *OpVal = Emit(Op, &Target);
3242 assert(OpVal == 0 && "Expected an aggregate operand!");
3243 break;
3246 // Lvalues
3247 case VAR_DECL:
3248 case PARM_DECL:
3249 case RESULT_DECL:
3250 case INDIRECT_REF:
3251 case ARRAY_REF:
3252 case ARRAY_RANGE_REF:
3253 case COMPONENT_REF:
3254 case BIT_FIELD_REF:
3255 case STRING_CST:
3256 case REALPART_EXPR:
3257 case IMAGPART_EXPR:
3258 // Same as EmitLoadOfLValue but taking the size from TREE_TYPE(exp), since
3259 // the size of TREE_TYPE(Op) may not be available.
3260 LValue LV = EmitLV(Op);
3261 assert(!LV.isBitfield() && "Expected an aggregate operand!");
3262 bool isVolatile = TREE_THIS_VOLATILE(Op);
3263 unsigned Alignment = LV.getAlignment();
3265 EmitAggregateCopy(Target, MemRef(LV.Ptr, Alignment, isVolatile),
3266 TREE_TYPE(exp));
3267 break;
3270 if (DestLoc)
3271 return 0;
3273 // Target holds the temporary created above.
3274 const Type *ExpTy = ConvertType(TREE_TYPE(exp));
3275 return Builder.CreateLoad(BitCastToType(Target.Ptr,
3276 ExpTy->getPointerTo()));
3279 if (DestLoc) {
3280 // The input is a scalar the output is an aggregate, just eval the input,
3281 // then store into DestLoc.
3282 Value *OpVal = Emit(Op, 0);
3283 assert(OpVal && "Expected a scalar result!");
3284 Value *Ptr = BitCastToType(DestLoc->Ptr, OpVal->getType()->getPointerTo());
3285 StoreInst *St = Builder.CreateStore(OpVal, Ptr, DestLoc->Volatile);
3286 St->setAlignment(DestLoc->getAlignment());
3287 return 0;
3290 // Otherwise, this is a scalar to scalar conversion.
3291 Value *OpVal = Emit(Op, 0);
3292 assert(OpVal && "Expected a scalar result!");
3293 const Type *DestTy = ConvertType(TREE_TYPE(exp));
3295 // If the source is a pointer, use ptrtoint to get it to something
3296 // bitcast'able. This supports things like v_c_e(foo*, float).
3297 if (isa<PointerType>(OpVal->getType())) {
3298 if (isa<PointerType>(DestTy)) // ptr->ptr is a simple bitcast.
3299 return Builder.CreateBitCast(OpVal, DestTy);
3300 // Otherwise, ptrtoint to intptr_t first.
3301 OpVal = Builder.CreatePtrToInt(OpVal, TD.getIntPtrType(Context));
3304 // If the destination type is a pointer, use inttoptr.
3305 if (isa<PointerType>(DestTy))
3306 return Builder.CreateIntToPtr(OpVal, DestTy);
3308 // Otherwise, use a bitcast.
3309 return Builder.CreateBitCast(OpVal, DestTy);
3312 Value *TreeToLLVM::EmitNEGATE_EXPR(tree exp, const MemRef *DestLoc) {
3313 if (!DestLoc) {
3314 Value *V = Emit(TREE_OPERAND(exp, 0), 0);
3315 if (V->getType()->isFPOrFPVector())
3316 return Builder.CreateFNeg(V);
3317 if (!isa<PointerType>(V->getType())) {
3318 bool HasNSW = !TYPE_UNSIGNED(TREE_TYPE(exp)) && !flag_wrapv;
3319 return HasNSW ? Builder.CreateNSWNeg(V) : Builder.CreateNeg(V);
3322 // GCC allows NEGATE_EXPR on pointers as well. Cast to int, negate, cast
3323 // back.
3324 V = CastToAnyType(V, false, TD.getIntPtrType(Context), false);
3325 V = Builder.CreateNeg(V);
3326 return CastToType(Instruction::IntToPtr, V, ConvertType(TREE_TYPE(exp)));
3329 // Emit the operand to a temporary.
3330 const Type *ComplexTy =
3331 cast<PointerType>(DestLoc->Ptr->getType())->getElementType();
3332 MemRef Tmp = CreateTempLoc(ComplexTy);
3333 Emit(TREE_OPERAND(exp, 0), &Tmp);
3335 // Handle complex numbers: -(a+ib) = -a + i*-b
3336 Value *R, *I;
3337 EmitLoadFromComplex(R, I, Tmp);
3338 if (R->getType()->isFloatingPoint()) {
3339 R = Builder.CreateFNeg(R);
3340 I = Builder.CreateFNeg(I);
3341 } else {
3342 R = Builder.CreateNeg(R);
3343 I = Builder.CreateNeg(I);
3345 EmitStoreToComplex(*DestLoc, R, I);
3346 return 0;
3349 Value *TreeToLLVM::EmitCONJ_EXPR(tree exp, const MemRef *DestLoc) {
3350 assert(DestLoc && "CONJ_EXPR only applies to complex numbers.");
3351 // Emit the operand to a temporary.
3352 const Type *ComplexTy =
3353 cast<PointerType>(DestLoc->Ptr->getType())->getElementType();
3354 MemRef Tmp = CreateTempLoc(ComplexTy);
3355 Emit(TREE_OPERAND(exp, 0), &Tmp);
3357 // Handle complex numbers: ~(a+ib) = a + i*-b
3358 Value *R, *I;
3359 EmitLoadFromComplex(R, I, Tmp);
3360 if (I->getType()->isFloatingPoint())
3361 I = Builder.CreateFNeg(I);
3362 else
3363 I = Builder.CreateNeg(I);
3364 EmitStoreToComplex(*DestLoc, R, I);
3365 return 0;
3368 Value *TreeToLLVM::EmitABS_EXPR(tree exp) {
3369 Value *Op = Emit(TREE_OPERAND(exp, 0), 0);
3370 if (!Op->getType()->isFloatingPoint()) {
3371 Value *OpN = Builder.CreateNeg(Op, (Op->getNameStr()+"neg").c_str());
3372 ICmpInst::Predicate pred = TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0))) ?
3373 ICmpInst::ICMP_UGE : ICmpInst::ICMP_SGE;
3374 Value *Cmp = Builder.CreateICmp(pred, Op,
3375 Constant::getNullValue(Op->getType()), "abscond");
3376 return Builder.CreateSelect(Cmp, Op, OpN, "abs");
3379 // Turn FP abs into fabs/fabsf.
3380 const char *Name = 0;
3382 switch (Op->getType()->getTypeID()) {
3383 default: assert(0 && "Unknown FP type!");
3384 case Type::FloatTyID: Name = "fabsf"; break;
3385 case Type::DoubleTyID: Name = "fabs"; break;
3386 case Type::X86_FP80TyID:
3387 case Type::PPC_FP128TyID:
3388 case Type::FP128TyID: Name = "fabsl"; break;
3391 Value *V = TheModule->getOrInsertFunction(Name, Op->getType(), Op->getType(),
3392 NULL);
3393 CallInst *Call = Builder.CreateCall(V, Op);
3394 Call->setDoesNotThrow();
3395 Call->setDoesNotAccessMemory();
3396 return Call;
3399 /// getSuitableBitCastIntType - Given Ty is a floating point type or a vector
3400 /// type with floating point elements, return an integer type to bitcast to.
3401 /// e.g. 4 x float -> 4 x i32
3402 static const Type *getSuitableBitCastIntType(const Type *Ty) {
3403 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
3404 unsigned NumElements = VTy->getNumElements();
3405 const Type *EltTy = VTy->getElementType();
3406 return VectorType::get(
3407 IntegerType::get(Context, EltTy->getPrimitiveSizeInBits()), NumElements);
3409 return IntegerType::get(Context, Ty->getPrimitiveSizeInBits());
3412 Value *TreeToLLVM::EmitBIT_NOT_EXPR(tree exp) {
3413 Value *Op = Emit(TREE_OPERAND(exp, 0), 0);
3414 const Type *Ty = Op->getType();
3415 if (isa<PointerType>(Ty)) {
3416 assert (TREE_CODE(TREE_TYPE(exp)) == INTEGER_TYPE &&
3417 "Expected integer type here");
3418 Ty = ConvertType(TREE_TYPE(exp));
3419 Op = CastToType(Instruction::PtrToInt, Op, Ty);
3420 } else if (Ty->isFloatingPoint() ||
3421 (isa<VectorType>(Ty) &&
3422 cast<VectorType>(Ty)->getElementType()->isFloatingPoint())) {
3423 Op = BitCastToType(Op, getSuitableBitCastIntType(Ty));
3425 return BitCastToType(Builder.CreateNot(Op,
3426 (Op->getNameStr()+"not").c_str()),Ty);
3429 Value *TreeToLLVM::EmitTRUTH_NOT_EXPR(tree exp) {
3430 Value *V = Emit(TREE_OPERAND(exp, 0), 0);
3431 if (V->getType() != Type::getInt1Ty(Context))
3432 V = Builder.CreateICmpNE(V,
3433 Constant::getNullValue(V->getType()), "toBool");
3434 V = Builder.CreateNot(V, (V->getNameStr()+"not").c_str());
3435 return CastToUIntType(V, ConvertType(TREE_TYPE(exp)));
3438 /// EmitCompare - 'exp' is a comparison of two values. Opc is the base LLVM
3439 /// comparison to use. isUnord is true if this is a floating point comparison
3440 /// that should also be true if either operand is a NaN. Note that Opc can be
3441 /// set to zero for special cases.
3443 /// If DestTy is specified, make sure to return the result with the specified
3444 /// integer type. Otherwise, return the expression as whatever TREE_TYPE(exp)
3445 /// corresponds to.
3446 Value *TreeToLLVM::EmitCompare(tree exp, unsigned UIOpc, unsigned SIOpc,
3447 unsigned FPPred, const Type *DestTy) {
3448 // Get the type of the operands
3449 tree Op0Ty = TREE_TYPE(TREE_OPERAND(exp,0));
3451 Value *Result;
3453 // Deal with complex types
3454 if (TREE_CODE(Op0Ty) == COMPLEX_TYPE) {
3455 Result = EmitComplexBinOp(exp, 0); // Complex ==/!=
3456 } else {
3457 // Get the compare operands, in the right type. Comparison of struct is not
3458 // allowed, so this is safe as we already handled complex (struct) type.
3459 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3460 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3461 bool LHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0)));
3462 bool RHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 1)));
3463 RHS = CastToAnyType(RHS, RHSIsSigned, LHS->getType(), LHSIsSigned);
3464 assert(LHS->getType() == RHS->getType() && "Binop type equality failure!");
3466 if (FLOAT_TYPE_P(Op0Ty)) {
3467 // Handle floating point comparisons, if we get here.
3468 Result = Builder.CreateFCmp(FCmpInst::Predicate(FPPred), LHS, RHS);
3469 } else {
3470 // Handle the integer/pointer cases. Determine which predicate to use based
3471 // on signedness.
3472 ICmpInst::Predicate pred =
3473 ICmpInst::Predicate(TYPE_UNSIGNED(Op0Ty) ? UIOpc : SIOpc);
3475 // Get the compare instructions
3476 Result = Builder.CreateICmp(pred, LHS, RHS);
3479 assert(Result->getType() == Type::getInt1Ty(Context) && "Expected i1 result for compare");
3481 if (DestTy == 0)
3482 DestTy = ConvertType(TREE_TYPE(exp));
3484 // The GCC type is probably an int, not a bool. ZExt to the right size.
3485 if (Result->getType() == DestTy)
3486 return Result;
3487 return Builder.CreateZExt(Result, DestTy);
3490 /// EmitBinOp - 'exp' is a binary operator.
3492 Value *TreeToLLVM::EmitBinOp(tree exp, const MemRef *DestLoc, unsigned Opc) {
3493 const Type *Ty = ConvertType(TREE_TYPE(exp));
3494 if (isa<PointerType>(Ty))
3495 return EmitPtrBinOp(exp, Opc); // Pointer arithmetic!
3496 if (isa<StructType>(Ty))
3497 return EmitComplexBinOp(exp, DestLoc);
3498 assert(Ty->isSingleValueType() && DestLoc == 0 &&
3499 "Bad binary operation!");
3501 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3502 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3504 // GCC has no problem with things like "xor uint X, int 17", and X-Y, where
3505 // X and Y are pointer types, but the result is an integer. As such, convert
3506 // everything to the result type.
3507 bool LHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0)));
3508 bool RHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 1)));
3509 bool TyIsSigned = !TYPE_UNSIGNED(TREE_TYPE(exp));
3510 bool IsExactDiv = TREE_CODE(exp) == EXACT_DIV_EXPR;
3512 LHS = CastToAnyType(LHS, LHSIsSigned, Ty, TyIsSigned);
3513 RHS = CastToAnyType(RHS, RHSIsSigned, Ty, TyIsSigned);
3515 // If it's And, Or, or Xor, make sure the operands are casted to the right
3516 // integer types first.
3517 bool isLogicalOp = Opc == Instruction::And || Opc == Instruction::Or ||
3518 Opc == Instruction::Xor;
3519 const Type *ResTy = Ty;
3520 if (isLogicalOp &&
3521 (Ty->isFloatingPoint() ||
3522 (isa<VectorType>(Ty) &&
3523 cast<VectorType>(Ty)->getElementType()->isFloatingPoint()))) {
3524 Ty = getSuitableBitCastIntType(Ty);
3525 LHS = BitCastToType(LHS, Ty);
3526 RHS = BitCastToType(RHS, Ty);
3529 Value *V;
3530 if (Opc == Instruction::SDiv && IsExactDiv)
3531 V = Builder.CreateExactSDiv(LHS, RHS);
3532 else if (Opc == Instruction::Add && TyIsSigned && !flag_wrapv)
3533 V = Builder.CreateNSWAdd(LHS, RHS);
3534 else if (Opc == Instruction::Sub && TyIsSigned && !flag_wrapv)
3535 V = Builder.CreateNSWSub(LHS, RHS);
3536 else if (Opc == Instruction::Mul && TyIsSigned && !flag_wrapv)
3537 V = Builder.CreateNSWMul(LHS, RHS);
3538 else
3539 V = Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);
3540 if (ResTy != Ty)
3541 V = BitCastToType(V, ResTy);
3542 return V;
3545 /// EmitPtrBinOp - Handle binary expressions involving pointers, e.g. "P+4".
3547 Value *TreeToLLVM::EmitPtrBinOp(tree exp, unsigned Opc) {
3548 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3550 // If this is an expression like (P+4), try to turn this into
3551 // "getelementptr P, 1".
3552 if ((Opc == Instruction::Add || Opc == Instruction::Sub) &&
3553 TREE_CODE(TREE_OPERAND(exp, 1)) == INTEGER_CST) {
3554 int64_t Offset = getINTEGER_CSTVal(TREE_OPERAND(exp, 1));
3556 // If POINTER_SIZE is 32-bits and the offset is signed, sign extend it.
3557 if (POINTER_SIZE == 32 && !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 1))))
3558 Offset = (Offset << 32) >> 32;
3560 // Figure out how large the element pointed to is.
3561 const Type *ElTy = cast<PointerType>(LHS->getType())->getElementType();
3562 // We can't get the type size (and thus convert to using a GEP instr) from
3563 // pointers to opaque structs if the type isn't abstract.
3564 if (ElTy->isSized()) {
3565 int64_t EltSize = TD.getTypeAllocSize(ElTy);
3567 // If EltSize exactly divides Offset, then we know that we can turn this
3568 // into a getelementptr instruction.
3569 int64_t EltOffset = EltSize ? Offset/EltSize : 0;
3570 if (EltOffset*EltSize == Offset) {
3571 // If this is a subtract, we want to step backwards.
3572 if (Opc == Instruction::Sub)
3573 EltOffset = -EltOffset;
3574 Constant *C = ConstantInt::get(Type::getInt64Ty(Context), EltOffset);
3575 Value *V = flag_wrapv ?
3576 Builder.CreateGEP(LHS, C) :
3577 Builder.CreateInBoundsGEP(LHS, C);
3578 return BitCastToType(V, ConvertType(TREE_TYPE(exp)));
3584 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3586 const Type *IntPtrTy = TD.getIntPtrType(Context);
3587 bool LHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0)));
3588 bool RHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 1)));
3589 LHS = CastToAnyType(LHS, LHSIsSigned, IntPtrTy, false);
3590 RHS = CastToAnyType(RHS, RHSIsSigned, IntPtrTy, false);
3591 Value *V = Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);
3592 return CastToType(Instruction::IntToPtr, V, ConvertType(TREE_TYPE(exp)));
3596 Value *TreeToLLVM::EmitTruthOp(tree exp, unsigned Opc) {
3597 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3598 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3600 // This is a truth operation like the strict &&,||,^^. Convert to bool as
3601 // a test against zero
3602 LHS = Builder.CreateICmpNE(LHS,
3603 Constant::getNullValue(LHS->getType()),
3604 "toBool");
3605 RHS = Builder.CreateICmpNE(RHS,
3606 Constant::getNullValue(RHS->getType()),
3607 "toBool");
3609 Value *Res = Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);
3610 return CastToType(Instruction::ZExt, Res, ConvertType(TREE_TYPE(exp)));
3614 Value *TreeToLLVM::EmitShiftOp(tree exp, const MemRef *DestLoc, unsigned Opc) {
3615 assert(DestLoc == 0 && "aggregate shift?");
3616 const Type *Ty = ConvertType(TREE_TYPE(exp));
3617 assert(!isa<PointerType>(Ty) && "Pointer arithmetic!?");
3619 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3620 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3621 if (RHS->getType() != LHS->getType())
3622 RHS = Builder.CreateIntCast(RHS, LHS->getType(), false,
3623 (RHS->getNameStr()+".cast").c_str());
3625 return Builder.CreateBinOp((Instruction::BinaryOps)Opc, LHS, RHS);
3628 Value *TreeToLLVM::EmitRotateOp(tree exp, unsigned Opc1, unsigned Opc2) {
3629 Value *In = Emit(TREE_OPERAND(exp, 0), 0);
3630 Value *Amt = Emit(TREE_OPERAND(exp, 1), 0);
3632 if (isa<PointerType>(In->getType())) {
3633 const Type *Ty =
3634 IntegerType::get(Context,
3635 TYPE_PRECISION(TREE_TYPE (TREE_OPERAND (exp, 0))));
3636 In = Builder.CreatePtrToInt(In, Ty,
3637 (In->getNameStr()+".cast").c_str());
3640 if (Amt->getType() != In->getType())
3641 Amt = Builder.CreateIntCast(Amt, In->getType(), false,
3642 (Amt->getNameStr()+".cast").c_str());
3644 Value *TypeSize =
3645 ConstantInt::get(In->getType(),
3646 In->getType()->getPrimitiveSizeInBits());
3648 // Do the two shifts.
3649 Value *V1 = Builder.CreateBinOp((Instruction::BinaryOps)Opc1, In, Amt);
3650 Value *OtherShift = Builder.CreateSub(TypeSize, Amt);
3651 Value *V2 = Builder.CreateBinOp((Instruction::BinaryOps)Opc2, In, OtherShift);
3653 // Or the two together to return them.
3654 Value *Merge = Builder.CreateOr(V1, V2);
3655 return CastToUIntType(Merge, ConvertType(TREE_TYPE(exp)));
3658 Value *TreeToLLVM::EmitMinMaxExpr(tree exp, unsigned UIPred, unsigned SIPred,
3659 unsigned FPPred) {
3660 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3661 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3663 const Type *Ty = ConvertType(TREE_TYPE(exp));
3665 // The LHS, RHS and Ty could be integer, floating or pointer typed. We need
3666 // to convert the LHS and RHS into the destination type before doing the
3667 // comparison. Use CastInst::getCastOpcode to get this right.
3668 bool TyIsSigned = !TYPE_UNSIGNED(TREE_TYPE(exp));
3669 bool LHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0)));
3670 bool RHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 1)));
3671 Instruction::CastOps opcode =
3672 CastInst::getCastOpcode(LHS, LHSIsSigned, Ty, TyIsSigned);
3673 LHS = CastToType(opcode, LHS, Ty);
3674 opcode = CastInst::getCastOpcode(RHS, RHSIsSigned, Ty, TyIsSigned);
3675 RHS = CastToType(opcode, RHS, Ty);
3677 Value *Compare;
3678 if (LHS->getType()->isFloatingPoint())
3679 Compare = Builder.CreateFCmp(FCmpInst::Predicate(FPPred), LHS, RHS);
3680 else if (TYPE_UNSIGNED(TREE_TYPE(exp)))
3681 Compare = Builder.CreateICmp(ICmpInst::Predicate(UIPred), LHS, RHS);
3682 else
3683 Compare = Builder.CreateICmp(ICmpInst::Predicate(SIPred), LHS, RHS);
3685 return Builder.CreateSelect(Compare, LHS, RHS,
3686 TREE_CODE(exp) == MAX_EXPR ? "max" : "min");
3689 Value *TreeToLLVM::EmitEXACT_DIV_EXPR(tree exp, const MemRef *DestLoc) {
3690 // Unsigned EXACT_DIV_EXPR -> normal udiv.
3691 if (TYPE_UNSIGNED(TREE_TYPE(exp)))
3692 return EmitBinOp(exp, DestLoc, Instruction::UDiv);
3694 // Otherwise, emit this as a normal signed divide.
3695 return EmitBinOp(exp, DestLoc, Instruction::SDiv);
3698 Value *TreeToLLVM::EmitFLOOR_MOD_EXPR(tree exp, const MemRef *DestLoc) {
3699 // Notation: FLOOR_MOD_EXPR <-> Mod, TRUNC_MOD_EXPR <-> Rem.
3701 // We express Mod in terms of Rem as follows: if RHS exactly divides LHS,
3702 // or the values of LHS and RHS have the same sign, then Mod equals Rem.
3703 // Otherwise Mod equals Rem + RHS. This means that LHS Mod RHS traps iff
3704 // LHS Rem RHS traps.
3705 if (TYPE_UNSIGNED(TREE_TYPE(exp)))
3706 // LHS and RHS values must have the same sign if their type is unsigned.
3707 return EmitBinOp(exp, DestLoc, Instruction::URem);
3709 const Type *Ty = ConvertType(TREE_TYPE(exp));
3710 Constant *Zero = ConstantInt::get(Ty, 0);
3712 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3713 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3715 // The two possible values for Mod.
3716 Value *Rem = Builder.CreateSRem(LHS, RHS, "rem");
3717 Value *RemPlusRHS = Builder.CreateAdd(Rem, RHS);
3719 // HaveSameSign: (LHS >= 0) == (RHS >= 0).
3720 Value *LHSIsPositive = Builder.CreateICmpSGE(LHS, Zero);
3721 Value *RHSIsPositive = Builder.CreateICmpSGE(RHS, Zero);
3722 Value *HaveSameSign = Builder.CreateICmpEQ(LHSIsPositive,RHSIsPositive);
3724 // RHS exactly divides LHS iff Rem is zero.
3725 Value *RemIsZero = Builder.CreateICmpEQ(Rem, Zero);
3727 Value *SameAsRem = Builder.CreateOr(HaveSameSign, RemIsZero);
3728 return Builder.CreateSelect(SameAsRem, Rem, RemPlusRHS, "mod");
3731 Value *TreeToLLVM::EmitCEIL_DIV_EXPR(tree exp) {
3732 // Notation: CEIL_DIV_EXPR <-> CDiv, TRUNC_DIV_EXPR <-> Div.
3734 // CDiv calculates LHS/RHS by rounding up to the nearest integer. In terms
3735 // of Div this means if the values of LHS and RHS have opposite signs or if
3736 // LHS is zero, then CDiv necessarily equals Div; and
3737 // LHS CDiv RHS = (LHS - Sign(RHS)) Div RHS + 1
3738 // otherwise.
3740 const Type *Ty = ConvertType(TREE_TYPE(exp));
3741 Constant *Zero = ConstantInt::get(Ty, 0);
3742 Constant *One = ConstantInt::get(Ty, 1);
3743 Constant *MinusOne = Constant::getAllOnesValue(Ty);
3745 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3746 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3748 if (!TYPE_UNSIGNED(TREE_TYPE(exp))) {
3749 // In the case of signed arithmetic, we calculate CDiv as follows:
3750 // LHS CDiv RHS = (LHS - Sign(RHS) * Offset) Div RHS + Offset,
3751 // where Offset is 1 if LHS and RHS have the same sign and LHS is
3752 // not zero, and 0 otherwise.
3754 // On some machines INT_MIN Div -1 traps. You might expect a trap for
3755 // INT_MIN CDiv -1 too, but this implementation will not generate one.
3756 // Quick quiz question: what value is returned for INT_MIN CDiv -1?
3758 // Determine the signs of LHS and RHS, and whether they have the same sign.
3759 Value *LHSIsPositive = Builder.CreateICmpSGE(LHS, Zero);
3760 Value *RHSIsPositive = Builder.CreateICmpSGE(RHS, Zero);
3761 Value *HaveSameSign = Builder.CreateICmpEQ(LHSIsPositive, RHSIsPositive);
3763 // Offset equals 1 if LHS and RHS have the same sign and LHS is not zero.
3764 Value *LHSNotZero = Builder.CreateICmpNE(LHS, Zero);
3765 Value *OffsetOne = Builder.CreateAnd(HaveSameSign, LHSNotZero);
3766 // ... otherwise it is 0.
3767 Value *Offset = Builder.CreateSelect(OffsetOne, One, Zero);
3769 // Calculate Sign(RHS) ...
3770 Value *SignRHS = Builder.CreateSelect(RHSIsPositive, One, MinusOne);
3771 // ... and Sign(RHS) * Offset
3772 Value *SignedOffset = CastToType(Instruction::SExt, OffsetOne, Ty);
3773 SignedOffset = Builder.CreateAnd(SignRHS, SignedOffset);
3775 // Return CDiv = (LHS - Sign(RHS) * Offset) Div RHS + Offset.
3776 Value *CDiv = Builder.CreateSub(LHS, SignedOffset);
3777 CDiv = Builder.CreateSDiv(CDiv, RHS);
3778 return Builder.CreateAdd(CDiv, Offset, "cdiv");
3781 // In the case of unsigned arithmetic, LHS and RHS necessarily have the
3782 // same sign, so we can use
3783 // LHS CDiv RHS = (LHS - 1) Div RHS + 1
3784 // as long as LHS is non-zero.
3786 // Offset is 1 if LHS is non-zero, 0 otherwise.
3787 Value *LHSNotZero = Builder.CreateICmpNE(LHS, Zero);
3788 Value *Offset = Builder.CreateSelect(LHSNotZero, One, Zero);
3790 // Return CDiv = (LHS - Offset) Div RHS + Offset.
3791 Value *CDiv = Builder.CreateSub(LHS, Offset);
3792 CDiv = Builder.CreateUDiv(CDiv, RHS);
3793 return Builder.CreateAdd(CDiv, Offset, "cdiv");
3796 Value *TreeToLLVM::EmitFLOOR_DIV_EXPR(tree exp) {
3797 // Notation: FLOOR_DIV_EXPR <-> FDiv, TRUNC_DIV_EXPR <-> Div.
3798 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3799 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3801 // FDiv calculates LHS/RHS by rounding down to the nearest integer. In terms
3802 // of Div this means if the values of LHS and RHS have the same sign or if LHS
3803 // is zero, then FDiv necessarily equals Div; and
3804 // LHS FDiv RHS = (LHS + Sign(RHS)) Div RHS - 1
3805 // otherwise.
3807 if (TYPE_UNSIGNED(TREE_TYPE(exp)))
3808 // In the case of unsigned arithmetic, LHS and RHS necessarily have the
3809 // same sign, so FDiv is the same as Div.
3810 return Builder.CreateUDiv(LHS, RHS, "fdiv");
3812 const Type *Ty = ConvertType(TREE_TYPE(exp));
3813 Constant *Zero = ConstantInt::get(Ty, 0);
3814 Constant *One = ConstantInt::get(Ty, 1);
3815 Constant *MinusOne = Constant::getAllOnesValue(Ty);
3817 // In the case of signed arithmetic, we calculate FDiv as follows:
3818 // LHS FDiv RHS = (LHS + Sign(RHS) * Offset) Div RHS - Offset,
3819 // where Offset is 1 if LHS and RHS have opposite signs and LHS is
3820 // not zero, and 0 otherwise.
3822 // Determine the signs of LHS and RHS, and whether they have the same sign.
3823 Value *LHSIsPositive = Builder.CreateICmpSGE(LHS, Zero);
3824 Value *RHSIsPositive = Builder.CreateICmpSGE(RHS, Zero);
3825 Value *SignsDiffer = Builder.CreateICmpNE(LHSIsPositive, RHSIsPositive);
3827 // Offset equals 1 if LHS and RHS have opposite signs and LHS is not zero.
3828 Value *LHSNotZero = Builder.CreateICmpNE(LHS, Zero);
3829 Value *OffsetOne = Builder.CreateAnd(SignsDiffer, LHSNotZero);
3830 // ... otherwise it is 0.
3831 Value *Offset = Builder.CreateSelect(OffsetOne, One, Zero);
3833 // Calculate Sign(RHS) ...
3834 Value *SignRHS = Builder.CreateSelect(RHSIsPositive, One, MinusOne);
3835 // ... and Sign(RHS) * Offset
3836 Value *SignedOffset = CastToType(Instruction::SExt, OffsetOne, Ty);
3837 SignedOffset = Builder.CreateAnd(SignRHS, SignedOffset);
3839 // Return FDiv = (LHS + Sign(RHS) * Offset) Div RHS - Offset.
3840 Value *FDiv = Builder.CreateAdd(LHS, SignedOffset);
3841 FDiv = Builder.CreateSDiv(FDiv, RHS);
3842 return Builder.CreateSub(FDiv, Offset, "fdiv");
3845 Value *TreeToLLVM::EmitROUND_DIV_EXPR(tree exp) {
3846 // Notation: ROUND_DIV_EXPR <-> RDiv, TRUNC_DIV_EXPR <-> Div.
3848 // RDiv calculates LHS/RHS by rounding to the nearest integer. Ties
3849 // are broken by rounding away from zero. In terms of Div this means:
3850 // LHS RDiv RHS = (LHS + (RHS Div 2)) Div RHS
3851 // if the values of LHS and RHS have the same sign; and
3852 // LHS RDiv RHS = (LHS - (RHS Div 2)) Div RHS
3853 // if the values of LHS and RHS differ in sign. The intermediate
3854 // expressions in these formulae can overflow, so some tweaking is
3855 // required to ensure correct results. The details depend on whether
3856 // we are doing signed or unsigned arithmetic.
3858 const Type *Ty = ConvertType(TREE_TYPE(exp));
3859 Constant *Zero = ConstantInt::get(Ty, 0);
3860 Constant *Two = ConstantInt::get(Ty, 2);
3862 Value *LHS = Emit(TREE_OPERAND(exp, 0), 0);
3863 Value *RHS = Emit(TREE_OPERAND(exp, 1), 0);
3865 if (!TYPE_UNSIGNED(TREE_TYPE(exp))) {
3866 // In the case of signed arithmetic, we calculate RDiv as follows:
3867 // LHS RDiv RHS = (sign) ( (|LHS| + (|RHS| UDiv 2)) UDiv |RHS| ),
3868 // where sign is +1 if LHS and RHS have the same sign, -1 if their
3869 // signs differ. Doing the computation unsigned ensures that there
3870 // is no overflow.
3872 // On some machines INT_MIN Div -1 traps. You might expect a trap for
3873 // INT_MIN RDiv -1 too, but this implementation will not generate one.
3874 // Quick quiz question: what value is returned for INT_MIN RDiv -1?
3876 // Determine the signs of LHS and RHS, and whether they have the same sign.
3877 Value *LHSIsPositive = Builder.CreateICmpSGE(LHS, Zero);
3878 Value *RHSIsPositive = Builder.CreateICmpSGE(RHS, Zero);
3879 Value *HaveSameSign = Builder.CreateICmpEQ(LHSIsPositive, RHSIsPositive);
3881 // Calculate |LHS| ...
3882 Value *MinusLHS = Builder.CreateNeg(LHS);
3883 Value *AbsLHS = Builder.CreateSelect(LHSIsPositive, LHS, MinusLHS,
3884 (LHS->getNameStr()+".abs").c_str());
3885 // ... and |RHS|
3886 Value *MinusRHS = Builder.CreateNeg(RHS);
3887 Value *AbsRHS = Builder.CreateSelect(RHSIsPositive, RHS, MinusRHS,
3888 (RHS->getNameStr()+".abs").c_str());
3890 // Calculate AbsRDiv = (|LHS| + (|RHS| UDiv 2)) UDiv |RHS|.
3891 Value *HalfAbsRHS = Builder.CreateUDiv(AbsRHS, Two);
3892 Value *Numerator = Builder.CreateAdd(AbsLHS, HalfAbsRHS);
3893 Value *AbsRDiv = Builder.CreateUDiv(Numerator, AbsRHS);
3895 // Return AbsRDiv or -AbsRDiv according to whether LHS and RHS have the
3896 // same sign or not.
3897 Value *MinusAbsRDiv = Builder.CreateNeg(AbsRDiv);
3898 return Builder.CreateSelect(HaveSameSign, AbsRDiv, MinusAbsRDiv, "rdiv");
3901 // In the case of unsigned arithmetic, LHS and RHS necessarily have the
3902 // same sign, however overflow is a problem. We want to use the formula
3903 // LHS RDiv RHS = (LHS + (RHS Div 2)) Div RHS,
3904 // but if LHS + (RHS Div 2) overflows then we get the wrong result. Since
3905 // the use of a conditional branch seems to be unavoidable, we choose the
3906 // simple solution of explicitly checking for overflow, and using
3907 // LHS RDiv RHS = ((LHS + (RHS Div 2)) - RHS) Div RHS + 1
3908 // if it occurred.
3910 // Usually the numerator is LHS + (RHS Div 2); calculate this.
3911 Value *HalfRHS = Builder.CreateUDiv(RHS, Two);
3912 Value *Numerator = Builder.CreateAdd(LHS, HalfRHS);
3914 // Did the calculation overflow?
3915 Value *Overflowed = Builder.CreateICmpULT(Numerator, HalfRHS);
3917 // If so, use (LHS + (RHS Div 2)) - RHS for the numerator instead.
3918 Value *AltNumerator = Builder.CreateSub(Numerator, RHS);
3919 Numerator = Builder.CreateSelect(Overflowed, AltNumerator, Numerator);
3921 // Quotient = Numerator / RHS.
3922 Value *Quotient = Builder.CreateUDiv(Numerator, RHS);
3924 // Return Quotient unless we overflowed, in which case return Quotient + 1.
3925 return Builder.CreateAdd(Quotient, CastToUIntType(Overflowed, Ty), "rdiv");
3928 //===----------------------------------------------------------------------===//
3929 // ... Exception Handling ...
3930 //===----------------------------------------------------------------------===//
3933 /// EmitEXC_PTR_EXPR - Handle EXC_PTR_EXPR.
3934 Value *TreeToLLVM::EmitEXC_PTR_EXPR(tree exp) {
3935 CreateExceptionValues();
3936 // Load exception address.
3937 Value *V = Builder.CreateLoad(ExceptionValue, "eh_value");
3938 // Cast the address to the right pointer type.
3939 return BitCastToType(V, ConvertType(TREE_TYPE(exp)));
3942 /// EmitFILTER_EXPR - Handle FILTER_EXPR.
3943 Value *TreeToLLVM::EmitFILTER_EXPR(tree exp) {
3944 CreateExceptionValues();
3945 // Load exception selector.
3946 return Builder.CreateLoad(ExceptionSelectorValue, "eh_select");
3949 /// EmitRESX_EXPR - Handle RESX_EXPR.
3950 Value *TreeToLLVM::EmitRESX_EXPR(tree exp) {
3951 unsigned RegionNo = TREE_INT_CST_LOW(TREE_OPERAND (exp, 0));
3952 std::vector<struct eh_region *> Handlers;
3954 foreach_reachable_handler(RegionNo, true, AddHandler, &Handlers);
3956 if (!Handlers.empty()) {
3957 for (std::vector<struct eh_region *>::iterator I = Handlers.begin(),
3958 E = Handlers.end(); I != E; ++I)
3959 // Create a post landing pad for the handler.
3960 getPostPad(get_eh_region_number(*I));
3962 Builder.CreateBr(getPostPad(get_eh_region_number(*Handlers.begin())));
3963 } else {
3964 assert(can_throw_external_1(RegionNo, true) &&
3965 "Must-not-throw region handled by runtime?");
3966 // Unwinding continues in the caller.
3967 if (!UnwindBB)
3968 UnwindBB = BasicBlock::Create(Context, "Unwind");
3969 Builder.CreateBr(UnwindBB);
3972 EmitBlock(BasicBlock::Create(Context, ""));
3973 return 0;
3976 //===----------------------------------------------------------------------===//
3977 // ... Inline Assembly and Register Variables ...
3978 //===----------------------------------------------------------------------===//
3981 /// Reads from register variables are handled by emitting an inline asm node
3982 /// that copies the value out of the specified register.
3983 Value *TreeToLLVM::EmitReadOfRegisterVariable(tree decl,
3984 const MemRef *DestLoc) {
3985 const Type *Ty = ConvertType(TREE_TYPE(decl));
3987 // If there was an error, return something bogus.
3988 if (ValidateRegisterVariable(decl)) {
3989 if (Ty->isSingleValueType())
3990 return UndefValue::get(Ty);
3991 return 0; // Just don't copy something into DestLoc.
3994 // Turn this into a 'tmp = call Ty asm "", "={reg}"()'.
3995 FunctionType *FTy = FunctionType::get(Ty, std::vector<const Type*>(),false);
3997 const char *Name = reg_names[decode_reg_name(extractRegisterName(decl))];
3999 InlineAsm *IA = InlineAsm::get(FTy, "", "={"+std::string(Name)+"}", false);
4000 CallInst *Call = Builder.CreateCall(IA);
4001 Call->setDoesNotThrow();
4002 return Call;
4005 /// Stores to register variables are handled by emitting an inline asm node
4006 /// that copies the value into the specified register.
4007 void TreeToLLVM::EmitModifyOfRegisterVariable(tree decl, Value *RHS) {
4008 // If there was an error, bail out.
4009 if (ValidateRegisterVariable(decl))
4010 return;
4012 // Turn this into a 'call void asm sideeffect "", "{reg}"(Ty %RHS)'.
4013 std::vector<const Type*> ArgTys;
4014 ArgTys.push_back(ConvertType(TREE_TYPE(decl)));
4015 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Context), ArgTys, false);
4017 const char *Name = reg_names[decode_reg_name(extractRegisterName(decl))];
4019 InlineAsm *IA = InlineAsm::get(FTy, "", "{"+std::string(Name)+"}", true);
4020 CallInst *Call = Builder.CreateCall(IA, RHS);
4021 Call->setDoesNotThrow();
4024 /// ConvertInlineAsmStr - Convert the specified inline asm string to an LLVM
4025 /// InlineAsm string. The GNU style inline asm template string has the
4026 /// following format:
4027 /// %N (for N a digit) means print operand N in usual manner.
4028 /// %= means a unique number for the inline asm.
4029 /// %lN means require operand N to be a CODE_LABEL or LABEL_REF
4030 /// and print the label name with no punctuation.
4031 /// %cN means require operand N to be a constant
4032 /// and print the constant expression with no punctuation.
4033 /// %aN means expect operand N to be a memory address
4034 /// (not a memory reference!) and print a reference to that address.
4035 /// %nN means expect operand N to be a constant and print a constant
4036 /// expression for minus the value of the operand, with no other
4037 /// punctuation.
4038 /// Other %xN expressions are turned into LLVM ${N:x} operands.
4040 static std::string ConvertInlineAsmStr(tree exp, unsigned NumOperands) {
4042 tree str = ASM_STRING(exp);
4043 if (TREE_CODE(str) == ADDR_EXPR) str = TREE_OPERAND(str, 0);
4045 // ASM_INPUT_P - This flag is set if this is a non-extended ASM, which means
4046 // that the asm string should not be interpreted, other than to escape $'s.
4047 if (ASM_INPUT_P(exp)) {
4048 const char *InStr = TREE_STRING_POINTER(str);
4049 std::string Result;
4050 while (1) {
4051 switch (*InStr++) {
4052 case 0: return Result; // End of string.
4053 default: Result += InStr[-1]; break; // Normal character.
4054 case '$': Result += "$$"; break; // Escape '$' characters.
4059 // Expand [name] symbolic operand names.
4060 str = resolve_asm_operand_names(str, ASM_OUTPUTS(exp), ASM_INPUTS(exp));
4062 const char *InStr = TREE_STRING_POINTER(str);
4064 std::string Result;
4065 while (1) {
4066 switch (*InStr++) {
4067 case 0: return Result; // End of string.
4068 default: Result += InStr[-1]; break; // Normal character.
4069 case '$': Result += "$$"; break; // Escape '$' characters.
4070 #ifdef ASSEMBLER_DIALECT
4071 // Note that we can't escape to ${, because that is the syntax for vars.
4072 case '{': Result += "$("; break; // Escape '{' character.
4073 case '}': Result += "$)"; break; // Escape '}' character.
4074 case '|': Result += "$|"; break; // Escape '|' character.
4075 #endif
4076 case '%': // GCC escape character.
4077 char EscapedChar = *InStr++;
4078 if (EscapedChar == '%') { // Escaped '%' character
4079 Result += '%';
4080 } else if (EscapedChar == '=') { // Unique ID for the asm instance.
4081 Result += "${:uid}";
4083 #ifdef LLVM_ASM_EXTENSIONS
4084 LLVM_ASM_EXTENSIONS(EscapedChar, InStr, Result)
4085 #endif
4086 else if (ISALPHA(EscapedChar)) {
4087 // % followed by a letter and some digits. This outputs an operand in a
4088 // special way depending on the letter. We turn this into LLVM ${N:o}
4089 // syntax.
4090 char *EndPtr;
4091 unsigned long OpNum = strtoul(InStr, &EndPtr, 10);
4093 if (InStr == EndPtr) {
4094 error("%Hoperand number missing after %%-letter",&EXPR_LOCATION(exp));
4095 return Result;
4096 } else if (OpNum >= NumOperands) {
4097 error("%Hoperand number out of range", &EXPR_LOCATION(exp));
4098 return Result;
4100 Result += "${" + utostr(OpNum) + ":" + EscapedChar + "}";
4101 InStr = EndPtr;
4102 } else if (ISDIGIT(EscapedChar)) {
4103 char *EndPtr;
4104 unsigned long OpNum = strtoul(InStr-1, &EndPtr, 10);
4105 InStr = EndPtr;
4106 Result += "$" + utostr(OpNum);
4107 #ifdef PRINT_OPERAND_PUNCT_VALID_P
4108 } else if (PRINT_OPERAND_PUNCT_VALID_P((unsigned char)EscapedChar)) {
4109 Result += "${:";
4110 Result += EscapedChar;
4111 Result += "}";
4112 #endif
4113 } else {
4114 output_operand_lossage("invalid %%-code");
4116 break;
4121 /// CanonicalizeConstraint - If we can canonicalize the constraint into
4122 /// something simpler, do so now. This turns register classes with a single
4123 /// register into the register itself, expands builtin constraints to multiple
4124 /// alternatives, etc.
4125 static std::string CanonicalizeConstraint(const char *Constraint) {
4126 std::string Result;
4128 // Skip over modifier characters.
4129 bool DoneModifiers = false;
4130 while (!DoneModifiers) {
4131 switch (*Constraint) {
4132 default: DoneModifiers = true; break;
4133 case '=': assert(0 && "Should be after '='s");
4134 case '+': assert(0 && "'+' should already be expanded");
4135 case '*':
4136 case '?':
4137 case '!':
4138 ++Constraint;
4139 break;
4140 case '&': // Pass earlyclobber to LLVM.
4141 case '%': // Pass commutative to LLVM.
4142 Result += *Constraint++;
4143 break;
4144 case '#': // No constraint letters left.
4145 return Result;
4149 while (*Constraint) {
4150 char ConstraintChar = *Constraint++;
4152 // 'g' is just short-hand for 'imr'.
4153 if (ConstraintChar == 'g') {
4154 Result += "imr";
4155 continue;
4158 // Translate 'p' to 'r'. This is supposed to check for a valid memory
4159 // address, but for inline assembly there is no way to know the mode of
4160 // the data being addressed. Assume that a general register is always
4161 // a valid address.
4162 if (ConstraintChar == 'p')
4163 ConstraintChar = 'r';
4165 // See if this is a regclass constraint.
4166 unsigned RegClass;
4167 if (ConstraintChar == 'r')
4168 // REG_CLASS_FROM_CONSTRAINT doesn't support 'r' for some reason.
4169 RegClass = GENERAL_REGS;
4170 else
4171 RegClass = REG_CLASS_FROM_CONSTRAINT(Constraint[-1], Constraint-1);
4173 if (RegClass == NO_REGS) { // not a reg class.
4174 Result += ConstraintChar;
4175 continue;
4178 // Look to see if the specified regclass has exactly one member, and if so,
4179 // what it is. Cache this information in AnalyzedRegClasses once computed.
4180 static std::map<unsigned, int> AnalyzedRegClasses;
4182 std::map<unsigned, int>::iterator I =
4183 AnalyzedRegClasses.lower_bound(RegClass);
4185 int RegMember;
4186 if (I != AnalyzedRegClasses.end() && I->first == RegClass) {
4187 // We've already computed this, reuse value.
4188 RegMember = I->second;
4189 } else {
4190 // Otherwise, scan the regclass, looking for exactly one member.
4191 RegMember = -1; // -1 => not a single-register class.
4192 for (unsigned j = 0; j != FIRST_PSEUDO_REGISTER; ++j)
4193 if (TEST_HARD_REG_BIT(reg_class_contents[RegClass], j)) {
4194 if (RegMember == -1) {
4195 RegMember = j;
4196 } else {
4197 RegMember = -1;
4198 break;
4201 // Remember this answer for the next query of this regclass.
4202 AnalyzedRegClasses.insert(I, std::make_pair(RegClass, RegMember));
4205 // If we found a single register register class, return the register.
4206 if (RegMember != -1) {
4207 Result += '{';
4208 Result += reg_names[RegMember];
4209 Result += '}';
4210 } else {
4211 Result += ConstraintChar;
4215 return Result;
4218 /// See if operand "exp" can use the indicated Constraint (which is
4219 /// terminated by a null or a comma).
4220 /// Returns: -1=no, 0=yes but auxiliary instructions needed, 1=yes and free
4221 static int MatchWeight(const char *Constraint, tree Operand) {
4222 const char *p = Constraint;
4223 int RetVal = 0;
4224 // Look for hard register operand. This matches only a constraint of a
4225 // register class that includes that hard register, and it matches that
4226 // perfectly, so we never return 0 in this case.
4227 if (TREE_CODE(Operand) == VAR_DECL && DECL_HARD_REGISTER(Operand)) {
4228 int RegNum = decode_reg_name(extractRegisterName(Operand));
4229 RetVal = -1;
4230 if (RegNum >= 0) {
4231 do {
4232 unsigned RegClass;
4233 if (*p == 'r')
4234 RegClass = GENERAL_REGS;
4235 else
4236 RegClass = REG_CLASS_FROM_CONSTRAINT(*p, p);
4237 if (RegClass != NO_REGS &&
4238 TEST_HARD_REG_BIT(reg_class_contents[RegClass], RegNum)) {
4239 RetVal = 1;
4240 break;
4242 ++p;
4243 } while (*p != ',' && *p != 0);
4246 // Look for integer constant operand. This cannot match "m", and "i" is
4247 // better than "r". FIXME target-dependent immediate letters are not handled
4248 // yet; in general they require looking at the value.
4249 if (TREE_CODE(Operand) == INTEGER_CST) {
4250 do {
4251 RetVal = -1;
4252 if (*p == 'i' || *p == 'n') { // integer constant
4253 RetVal = 1;
4254 break;
4256 if (*p != 'm' && *p != 'o' && *p != 'V') // not memory
4257 RetVal = 0;
4258 ++p;
4259 } while (*p != ',' && *p != 0);
4261 /// TEMPORARY. This has the effect that alternative 0 is always chosen,
4262 /// except in the cases handled above.
4263 return RetVal;
4266 /// ChooseConstraintTuple: we know each of the NumInputs+NumOutputs strings
4267 /// in Constraints[] is a comma-separated list of NumChoices different
4268 /// constraints. Look through the operands and constraint possibilities
4269 /// and pick a tuple where all the operands match. Replace the strings
4270 /// in Constraints[] with the shorter strings from that tuple (malloc'ed,
4271 /// caller is responsible for cleaning it up). Later processing can alter what
4272 /// Constraints points to, so to make sure we delete everything, the addresses
4273 /// of everything we allocated also are returned in ReplacementStrings.
4274 /// Casting back and forth from char* to const char* is Ugly, but we have to
4275 /// interface with C code that expects const char*.
4277 /// gcc's algorithm for picking "the best" tuple is quite complicated, and
4278 /// is performed after things like SROA, not before. At the moment we are
4279 /// just trying to pick one that will work. This may get refined.
4280 static void
4281 ChooseConstraintTuple (const char **Constraints, tree exp, unsigned NumInputs,
4282 unsigned NumOutputs, unsigned NumChoices,
4283 const char **ReplacementStrings)
4285 int MaxWeight = -1;
4286 unsigned int CommasToSkip = 0;
4287 int *Weights = (int *)alloca(NumChoices * sizeof(int));
4288 // RunningConstraints is pointers into the Constraints strings which
4289 // are incremented as we go to point to the beginning of each
4290 // comma-separated alternative.
4291 const char** RunningConstraints =
4292 (const char**)alloca((NumInputs+NumOutputs)*sizeof(const char*));
4293 memcpy(RunningConstraints, Constraints,
4294 (NumInputs+NumOutputs) * sizeof(const char*));
4295 // The entire point of this loop is to compute CommasToSkip.
4296 for (unsigned int i=0; i<NumChoices; i++) {
4297 Weights[i] = 0;
4298 unsigned int j = 0;
4299 for (tree Output = ASM_OUTPUTS(exp); j<NumOutputs;
4300 j++, Output = TREE_CHAIN(Output)) {
4301 if (i==0)
4302 RunningConstraints[j]++; // skip leading =
4303 const char* p = RunningConstraints[j];
4304 while (*p=='*' || *p=='&' || *p=='%') // skip modifiers
4305 p++;
4306 if (Weights[i] != -1) {
4307 int w = MatchWeight(p, TREE_VALUE(Output));
4308 // Nonmatch means the entire tuple doesn't match. However, we
4309 // keep scanning to set up RunningConstraints correctly for the
4310 // next tuple.
4311 if (w < 0)
4312 Weights[i] = -1;
4313 else
4314 Weights[i] += w;
4316 while (*p!=0 && *p!=',')
4317 p++;
4318 if (*p!=0) {
4319 p++; // skip comma
4320 while (*p=='*' || *p=='&' || *p=='%')
4321 p++; // skip modifiers
4323 RunningConstraints[j] = p;
4325 assert(j==NumOutputs);
4326 for (tree Input = ASM_INPUTS(exp); j<NumInputs+NumOutputs;
4327 j++, Input = TREE_CHAIN(Input)) {
4328 const char* p = RunningConstraints[j];
4329 if (Weights[i] != -1) {
4330 int w = MatchWeight(p, TREE_VALUE(Input));
4331 if (w < 0)
4332 Weights[i] = -1; // As above.
4333 else
4334 Weights[i] += w;
4336 while (*p!=0 && *p!=',')
4337 p++;
4338 if (*p!=0)
4339 p++;
4340 RunningConstraints[j] = p;
4342 if (Weights[i]>MaxWeight) {
4343 CommasToSkip = i;
4344 MaxWeight = Weights[i];
4347 // We have picked an alternative (the CommasToSkip'th one).
4348 // Change Constraints to point to malloc'd copies of the appropriate
4349 // constraints picked out of the original strings.
4350 for (unsigned int i=0; i<NumInputs+NumOutputs; i++) {
4351 assert(*(RunningConstraints[i])==0); // sanity check
4352 const char* start = Constraints[i];
4353 if (i<NumOutputs)
4354 start++; // skip '=' or '+'
4355 const char* end = start;
4356 while (*end != ',' && *end != 0)
4357 end++;
4358 for (unsigned int j=0; j<CommasToSkip; j++) {
4359 start = end+1;
4360 end = start;
4361 while (*end != ',' && *end != 0)
4362 end++;
4364 // String we want is at start..end-1 inclusive.
4365 // For outputs, copy the leading = or +.
4366 char *newstring;
4367 if (i<NumOutputs) {
4368 newstring = (char *)xmalloc(end-start+1+1);
4369 newstring[0] = *(Constraints[i]);
4370 strncpy(newstring+1, start, end-start);
4371 newstring[end-start+1] = 0;
4372 } else {
4373 newstring = (char *)xmalloc(end-start+1);
4374 strncpy(newstring, start, end-start);
4375 newstring[end-start] = 0;
4377 Constraints[i] = (const char *)newstring;
4378 ReplacementStrings[i] = (const char*)newstring;
4382 static void FreeConstTupleStrings(const char **ReplacementStrings,
4383 unsigned int Size) {
4384 for (unsigned int i=0; i<Size; i++)
4385 free((char *)ReplacementStrings[i]);
4388 // When extracting a register name from a DECL_HARD_REGISTER variable,
4389 // we normally want to look up RegNum in reg_names. This works on most
4390 // targets, where ADDITIONAL_REGISTER_NAMES are true synonyms. It does not
4391 // work on x86, where ADDITIONAL_REGISTER_NAMES are overlapping subregisters;
4392 // in particular AH and AL can't be distinguished if we go through reg_names.
4393 static const char* getConstraintRegNameFromGccTables(const char *RegName,
4394 unsigned int RegNum) {
4395 #ifdef LLVM_DO_NOT_USE_REG_NAMES
4396 (void)RegNum;
4397 if (*RegName == '%')
4398 RegName++;
4399 return RegName;
4400 #else
4401 return reg_names[RegNum];
4402 #endif
4405 Value *TreeToLLVM::EmitASM_EXPR(tree exp) {
4406 unsigned NumInputs = list_length(ASM_INPUTS(exp));
4407 unsigned NumOutputs = list_length(ASM_OUTPUTS(exp));
4408 unsigned NumInOut = 0;
4410 // Look for multiple alternative constraints: multiple alternatives separated
4411 // by commas.
4412 unsigned NumChoices = 0; // sentinal; real value is always at least 1.
4413 const char* p;
4414 for (tree t = ASM_INPUTS(exp); t; t = TREE_CHAIN(t)) {
4415 unsigned NumInputChoices = 1;
4416 for (p = TREE_STRING_POINTER(TREE_VALUE(TREE_PURPOSE(t))); *p; p++) {
4417 if (*p == ',')
4418 NumInputChoices++;
4420 if (NumChoices==0)
4421 NumChoices = NumInputChoices;
4422 else if (NumChoices != NumInputChoices)
4423 abort(); // invalid constraints
4425 for (tree t = ASM_OUTPUTS(exp); t; t = TREE_CHAIN(t)) {
4426 unsigned NumOutputChoices = 1;
4427 for (p = TREE_STRING_POINTER(TREE_VALUE(TREE_PURPOSE(t))); *p; p++) {
4428 if (*p == ',')
4429 NumOutputChoices++;
4431 if (NumChoices==0)
4432 NumChoices = NumOutputChoices;
4433 else if (NumChoices != NumOutputChoices)
4434 abort(); // invalid constraints
4437 /// Constraints - The output/input constraints, concatenated together in array
4438 /// form instead of list form.
4439 const char **Constraints =
4440 (const char **)alloca((NumOutputs + NumInputs) * sizeof(const char *));
4442 // Process outputs.
4443 int ValNum = 0;
4444 for (tree Output = ASM_OUTPUTS(exp); Output;
4445 Output = TREE_CHAIN(Output), ++ValNum) {
4446 tree Operand = TREE_VALUE(Output);
4447 tree type = TREE_TYPE(Operand);
4448 // If there's an erroneous arg, emit no insn.
4449 if (type == error_mark_node) return 0;
4451 // Parse the output constraint.
4452 const char *Constraint =
4453 TREE_STRING_POINTER(TREE_VALUE(TREE_PURPOSE(Output)));
4454 Constraints[ValNum] = Constraint;
4456 // Process inputs.
4457 for (tree Input = ASM_INPUTS(exp); Input; Input = TREE_CHAIN(Input),++ValNum){
4458 tree Val = TREE_VALUE(Input);
4459 tree type = TREE_TYPE(Val);
4460 // If there's an erroneous arg, emit no insn.
4461 if (type == error_mark_node) return 0;
4463 const char *Constraint =
4464 TREE_STRING_POINTER(TREE_VALUE(TREE_PURPOSE(Input)));
4465 Constraints[ValNum] = Constraint;
4468 // If there are multiple constraint tuples, pick one. Constraints is
4469 // altered to point to shorter strings (which are malloc'ed), and everything
4470 // below Just Works as in the NumChoices==1 case.
4471 const char** ReplacementStrings = 0;
4472 if (NumChoices>1) {
4473 ReplacementStrings =
4474 (const char **)alloca((NumOutputs + NumInputs) * sizeof(const char *));
4475 ChooseConstraintTuple(Constraints, exp, NumInputs, NumOutputs, NumChoices,
4476 ReplacementStrings);
4479 std::vector<Value*> CallOps;
4480 std::vector<const Type*> CallArgTypes;
4481 std::string NewAsmStr = ConvertInlineAsmStr(exp, NumOutputs+NumInputs);
4482 std::string ConstraintStr;
4484 // StoreCallResultAddr - The pointer to store the result of the call through.
4485 SmallVector<Value *, 4> StoreCallResultAddrs;
4486 SmallVector<const Type *, 4> CallResultTypes;
4487 SmallVector<bool, 4> CallResultIsSigned;
4489 // Process outputs.
4490 ValNum = 0;
4491 for (tree Output = ASM_OUTPUTS(exp); Output;
4492 Output = TREE_CHAIN(Output), ++ValNum) {
4493 tree Operand = TREE_VALUE(Output);
4495 // Parse the output constraint.
4496 const char *Constraint = Constraints[ValNum];
4497 bool IsInOut, AllowsReg, AllowsMem;
4498 if (!parse_output_constraint(&Constraint, ValNum, NumInputs, NumOutputs,
4499 &AllowsMem, &AllowsReg, &IsInOut)) {
4500 if (NumChoices>1)
4501 FreeConstTupleStrings(ReplacementStrings, NumInputs+NumOutputs);
4502 return 0;
4504 assert(Constraint[0] == '=' && "Not an output constraint?");
4506 // Output constraints must be addressable if they aren't simple register
4507 // constraints (this emits "address of register var" errors, etc).
4508 if (!AllowsReg && (AllowsMem || IsInOut))
4509 lang_hooks.mark_addressable(Operand);
4511 // Count the number of "+" constraints.
4512 if (IsInOut)
4513 ++NumInOut, ++NumInputs;
4515 std::string SimplifiedConstraint;
4516 // If this output register is pinned to a machine register, use that machine
4517 // register instead of the specified constraint.
4518 if (TREE_CODE(Operand) == VAR_DECL && DECL_HARD_REGISTER(Operand)) {
4519 const char* RegName = extractRegisterName(Operand);
4520 int RegNum = decode_reg_name(RegName);
4521 if (RegNum >= 0) {
4522 RegName = getConstraintRegNameFromGccTables(RegName, RegNum);
4523 unsigned RegNameLen = strlen(RegName);
4524 char *NewConstraint = (char*)alloca(RegNameLen+4);
4525 NewConstraint[0] = '=';
4526 NewConstraint[1] = '{';
4527 memcpy(NewConstraint+2, RegName, RegNameLen);
4528 NewConstraint[RegNameLen+2] = '}';
4529 NewConstraint[RegNameLen+3] = 0;
4530 SimplifiedConstraint = NewConstraint;
4531 // We should no longer consider mem constraints.
4532 AllowsMem = false;
4533 } else {
4534 // If we can simplify the constraint into something else, do so now.
4535 // This avoids LLVM having to know about all the (redundant) GCC
4536 // constraints.
4537 SimplifiedConstraint = CanonicalizeConstraint(Constraint+1);
4539 } else {
4540 SimplifiedConstraint = CanonicalizeConstraint(Constraint+1);
4543 LValue Dest = EmitLV(Operand);
4544 const Type *DestValTy =
4545 cast<PointerType>(Dest.Ptr->getType())->getElementType();
4547 assert(!Dest.isBitfield() && "Cannot assign into a bitfield!");
4548 if (!AllowsMem && DestValTy->isSingleValueType()) {// Reg dest -> asm return
4549 StoreCallResultAddrs.push_back(Dest.Ptr);
4550 ConstraintStr += ",=";
4551 ConstraintStr += SimplifiedConstraint;
4552 CallResultTypes.push_back(DestValTy);
4553 CallResultIsSigned.push_back(!TYPE_UNSIGNED(TREE_TYPE(Operand)));
4554 } else {
4555 ConstraintStr += ",=*";
4556 ConstraintStr += SimplifiedConstraint;
4557 CallOps.push_back(Dest.Ptr);
4558 CallArgTypes.push_back(Dest.Ptr->getType());
4562 // Process inputs.
4563 for (tree Input = ASM_INPUTS(exp); Input; Input = TREE_CHAIN(Input),++ValNum){
4564 tree Val = TREE_VALUE(Input);
4565 tree type = TREE_TYPE(Val);
4567 const char *Constraint = Constraints[ValNum];
4569 bool AllowsReg, AllowsMem;
4570 if (!parse_input_constraint(Constraints+ValNum, ValNum-NumOutputs,
4571 NumInputs, NumOutputs, NumInOut,
4572 Constraints, &AllowsMem, &AllowsReg)) {
4573 if (NumChoices>1)
4574 FreeConstTupleStrings(ReplacementStrings, NumInputs+NumOutputs);
4575 return 0;
4577 bool isIndirect = false;
4578 if (AllowsReg || !AllowsMem) { // Register operand.
4579 const Type *LLVMTy = ConvertType(type);
4581 Value *Op = 0;
4582 if (LLVMTy->isSingleValueType()) {
4583 if (TREE_CODE(Val)==ADDR_EXPR &&
4584 TREE_CODE(TREE_OPERAND(Val,0))==LABEL_DECL) {
4585 // Emit the label, but do not assume it is going to be the target
4586 // of an indirect branch. Having this logic here is a hack; there
4587 // should be a bit in the label identifying it as in an asm.
4588 Op = getLabelDeclBlock(TREE_OPERAND(Val, 0));
4589 } else
4590 Op = Emit(Val, 0);
4591 } else {
4592 LValue LV = EmitLV(Val);
4593 assert(!LV.isBitfield() && "Inline asm can't have bitfield operand");
4595 // Small structs and unions can be treated as integers.
4596 uint64_t TySize = TD.getTypeSizeInBits(LLVMTy);
4597 if (TySize == 1 || TySize == 8 || TySize == 16 ||
4598 TySize == 32 || TySize == 64 || (TySize == 128 && !AllowsMem)) {
4599 LLVMTy = IntegerType::get(Context, TySize);
4600 Op = Builder.CreateLoad(BitCastToType(LV.Ptr,
4601 LLVMTy->getPointerTo()));
4602 } else {
4603 // Codegen only supports indirect operands with mem constraints.
4604 if (!AllowsMem)
4605 error("%Haggregate does not match inline asm register constraint",
4606 &EXPR_LOCATION(exp));
4607 // Otherwise, emit our value as a lvalue.
4608 isIndirect = true;
4609 Op = LV.Ptr;
4613 const Type *OpTy = Op->getType();
4614 // If this input operand is matching an output operand, e.g. '0', check if
4615 // this is something that llvm supports. If the operand types are
4616 // different, then emit an error if 1) one of the types is not integer or
4617 // pointer, 2) if size of input type is larger than the output type. If
4618 // the size of the integer input size is smaller than the integer output
4619 // type, then cast it to the larger type and shift the value if the target
4620 // is big endian.
4621 if (ISDIGIT(Constraint[0])) {
4622 unsigned Match = atoi(Constraint);
4623 const Type *OTy = (Match < CallResultTypes.size())
4624 ? CallResultTypes[Match] : 0;
4625 if (OTy && OTy != OpTy) {
4626 if (!(isa<IntegerType>(OTy) || isa<PointerType>(OTy)) ||
4627 !(isa<IntegerType>(OpTy) || isa<PointerType>(OpTy))) {
4628 error("%Hunsupported inline asm: input constraint with a matching "
4629 "output constraint of incompatible type!",
4630 &EXPR_LOCATION(exp));
4631 if (NumChoices>1)
4632 FreeConstTupleStrings(ReplacementStrings, NumInputs+NumOutputs);
4633 return 0;
4635 unsigned OTyBits = TD.getTypeSizeInBits(OTy);
4636 unsigned OpTyBits = TD.getTypeSizeInBits(OpTy);
4637 if (OTyBits == 0 || OpTyBits == 0 || OTyBits < OpTyBits) {
4638 // It's tempting to implement the OTyBits < OpTyBits case by truncating
4639 // Op down to OTy, however that breaks in the case of an inline asm
4640 // constraint that corresponds to a single register, because the
4641 // user can write code that assumes the whole register is defined,
4642 // despite the output operand being only a subset of the register. For
4643 // example:
4645 // asm ("sarl $10, %%eax" : "=a"(c) : "0"(1000000));
4647 // The expected behavior is for %eax to be fully defined with the value
4648 // 1000000 immediately before the asm.
4649 error("%Hunsupported inline asm: input constraint with a matching "
4650 "output constraint of incompatible type!",
4651 &EXPR_LOCATION(exp));
4652 return 0;
4653 } else if (OTyBits > OpTyBits) {
4654 Op = CastToAnyType(Op, !TYPE_UNSIGNED(type),
4655 OTy, CallResultIsSigned[Match]);
4656 if (BYTES_BIG_ENDIAN) {
4657 Constant *ShAmt = ConstantInt::get(Op->getType(),
4658 OTyBits-OpTyBits);
4659 Op = Builder.CreateLShr(Op, ShAmt);
4661 OpTy = Op->getType();
4666 CallOps.push_back(Op);
4667 CallArgTypes.push_back(OpTy);
4668 } else { // Memory operand.
4669 lang_hooks.mark_addressable(TREE_VALUE(Input));
4670 isIndirect = true;
4671 LValue Src = EmitLV(Val);
4672 assert(!Src.isBitfield() && "Cannot read from a bitfield!");
4673 CallOps.push_back(Src.Ptr);
4674 CallArgTypes.push_back(Src.Ptr->getType());
4677 ConstraintStr += ',';
4678 if (isIndirect)
4679 ConstraintStr += '*';
4681 // If this output register is pinned to a machine register, use that machine
4682 // register instead of the specified constraint.
4683 if (TREE_CODE(Val) == VAR_DECL && DECL_HARD_REGISTER(Val)) {
4684 const char *RegName = extractRegisterName(Val);
4685 int RegNum = decode_reg_name(RegName);
4686 if (RegNum >= 0) {
4687 RegName = getConstraintRegNameFromGccTables(RegName, RegNum);
4688 ConstraintStr += '{';
4689 ConstraintStr += RegName;
4690 ConstraintStr += '}';
4691 continue;
4695 // If there is a simpler form for the register constraint, use it.
4696 std::string Simplified = CanonicalizeConstraint(Constraint);
4697 ConstraintStr += Simplified;
4700 // ASM_USES contains info about certain hard regs which are used as inputs.
4701 // gcc represents the xH registers on x86 this way because of deficiencies
4702 // in the way gcc can represent registers internally. llvm-gcc can represent
4703 // these as normal inputs, so we aren't using ASM_USES.
4704 assert(ASM_USES(exp)==0);
4706 // Process clobbers.
4708 // Some targets automatically clobber registers across an asm.
4709 tree Clobbers = targetm.md_asm_clobbers(ASM_OUTPUTS(exp), ASM_INPUTS(exp),
4710 ASM_CLOBBERS(exp));
4711 for (; Clobbers; Clobbers = TREE_CHAIN(Clobbers)) {
4712 const char *RegName = TREE_STRING_POINTER(TREE_VALUE(Clobbers));
4713 int RegCode = decode_reg_name(RegName);
4715 switch (RegCode) {
4716 case -1: // Nothing specified?
4717 case -2: // Invalid.
4718 error("%Hunknown register name %qs in %<asm%>", &EXPR_LOCATION(exp),
4719 RegName);
4720 if (NumChoices>1)
4721 FreeConstTupleStrings(ReplacementStrings, NumInputs+NumOutputs);
4722 return 0;
4723 case -3: // cc
4724 ConstraintStr += ",~{cc}";
4725 break;
4726 case -4: // memory
4727 ConstraintStr += ",~{memory}";
4728 break;
4729 default: // Normal register name.
4730 RegName = getConstraintRegNameFromGccTables(RegName, RegCode);
4731 ConstraintStr += ",~{";
4732 ConstraintStr += RegName;
4733 ConstraintStr += "}";
4734 break;
4738 const Type *CallResultType;
4739 switch (CallResultTypes.size()) {
4740 case 0: CallResultType = Type::getVoidTy(Context); break;
4741 case 1: CallResultType = CallResultTypes[0]; break;
4742 default:
4743 std::vector<const Type*> TmpVec(CallResultTypes.begin(),
4744 CallResultTypes.end());
4745 CallResultType = StructType::get(Context, TmpVec);
4746 break;
4749 const FunctionType *FTy =
4750 FunctionType::get(CallResultType, CallArgTypes, false);
4752 // Remove the leading comma if we have operands.
4753 if (!ConstraintStr.empty())
4754 ConstraintStr.erase(ConstraintStr.begin());
4756 // Make sure we're created a valid inline asm expression.
4757 if (!InlineAsm::Verify(FTy, ConstraintStr)) {
4758 error("%HInvalid or unsupported inline assembly!", &EXPR_LOCATION(exp));
4759 if (NumChoices>1)
4760 FreeConstTupleStrings(ReplacementStrings, NumInputs+NumOutputs);
4761 return 0;
4764 Value *Asm = InlineAsm::get(FTy, NewAsmStr, ConstraintStr,
4765 ASM_VOLATILE_P(exp) || !ASM_OUTPUTS(exp),
4766 ASM_ASM_BLOCK(exp));
4767 CallInst *CV = Builder.CreateCall(Asm, CallOps.begin(), CallOps.end(),
4768 CallResultTypes.empty() ? "" : "asmtmp");
4769 CV->setDoesNotThrow();
4771 // If the call produces a value, store it into the destination.
4772 if (StoreCallResultAddrs.size() == 1)
4773 Builder.CreateStore(CV, StoreCallResultAddrs[0]);
4774 else if (unsigned NumResults = StoreCallResultAddrs.size()) {
4775 for (unsigned i = 0; i != NumResults; ++i) {
4776 Value *ValI = Builder.CreateExtractValue(CV, i, "asmresult");
4777 Builder.CreateStore(ValI, StoreCallResultAddrs[i]);
4781 // Give the backend a chance to upgrade the inline asm to LLVM code. This
4782 // handles some common cases that LLVM has intrinsics for, e.g. x86 bswap ->
4783 // llvm.bswap.
4784 if (const TargetLowering *TLI = TheTarget->getTargetLowering())
4785 TLI->ExpandInlineAsm(CV);
4787 if (NumChoices>1)
4788 FreeConstTupleStrings(ReplacementStrings, NumInputs+NumOutputs);
4789 return 0;
4792 //===----------------------------------------------------------------------===//
4793 // ... Helpers for Builtin Function Expansion ...
4794 //===----------------------------------------------------------------------===//
4796 Value *TreeToLLVM::BuildVector(const std::vector<Value*> &Ops) {
4797 assert((Ops.size() & (Ops.size()-1)) == 0 &&
4798 "Not a power-of-two sized vector!");
4799 bool AllConstants = true;
4800 for (unsigned i = 0, e = Ops.size(); i != e && AllConstants; ++i)
4801 AllConstants &= isa<Constant>(Ops[i]);
4803 // If this is a constant vector, create a ConstantVector.
4804 if (AllConstants) {
4805 std::vector<Constant*> CstOps;
4806 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
4807 CstOps.push_back(cast<Constant>(Ops[i]));
4808 return ConstantVector::get(CstOps);
4811 // Otherwise, insertelement the values to build the vector.
4812 Value *Result =
4813 UndefValue::get(VectorType::get(Ops[0]->getType(), Ops.size()));
4815 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
4816 Result = Builder.CreateInsertElement(Result, Ops[i],
4817 ConstantInt::get(Type::getInt32Ty(Context), i));
4819 return Result;
4822 /// BuildVector - This varargs function builds a literal vector ({} syntax) with
4823 /// the specified null-terminated list of elements. The elements must be all
4824 /// the same element type and there must be a power of two of them.
4825 Value *TreeToLLVM::BuildVector(Value *Elt, ...) {
4826 std::vector<Value*> Ops;
4827 va_list VA;
4828 va_start(VA, Elt);
4830 Ops.push_back(Elt);
4831 while (Value *Arg = va_arg(VA, Value *))
4832 Ops.push_back(Arg);
4833 va_end(VA);
4835 return BuildVector(Ops);
4838 /// BuildVectorShuffle - Given two vectors and a variable length list of int
4839 /// constants, create a shuffle of the elements of the inputs, where each dest
4840 /// is specified by the indexes. The int constant list must be as long as the
4841 /// number of elements in the input vector.
4843 /// Undef values may be specified by passing in -1 as the result value.
4845 Value *TreeToLLVM::BuildVectorShuffle(Value *InVec1, Value *InVec2, ...) {
4846 assert(isa<VectorType>(InVec1->getType()) &&
4847 InVec1->getType() == InVec2->getType() && "Invalid shuffle!");
4848 unsigned NumElements = cast<VectorType>(InVec1->getType())->getNumElements();
4850 // Get all the indexes from varargs.
4851 std::vector<Constant*> Idxs;
4852 va_list VA;
4853 va_start(VA, InVec2);
4854 for (unsigned i = 0; i != NumElements; ++i) {
4855 int idx = va_arg(VA, int);
4856 if (idx == -1)
4857 Idxs.push_back(UndefValue::get(Type::getInt32Ty(Context)));
4858 else {
4859 assert((unsigned)idx < 2*NumElements && "Element index out of range!");
4860 Idxs.push_back(ConstantInt::get(Type::getInt32Ty(Context), idx));
4863 va_end(VA);
4865 // Turn this into the appropriate shuffle operation.
4866 return Builder.CreateShuffleVector(InVec1, InVec2,
4867 ConstantVector::get(Idxs));
4870 //===----------------------------------------------------------------------===//
4871 // ... Builtin Function Expansion ...
4872 //===----------------------------------------------------------------------===//
4874 /// EmitFrontendExpandedBuiltinCall - For MD builtins that do not have a
4875 /// directly corresponding LLVM intrinsic, we allow the target to do some amount
4876 /// of lowering. This allows us to avoid having intrinsics for operations that
4877 /// directly correspond to LLVM constructs.
4879 /// This method returns true if the builtin is handled, otherwise false.
4881 bool TreeToLLVM::EmitFrontendExpandedBuiltinCall(tree exp, tree fndecl,
4882 const MemRef *DestLoc,
4883 Value *&Result) {
4884 #ifdef LLVM_TARGET_INTRINSIC_LOWER
4885 // Get the result type and operand line in an easy to consume format.
4886 const Type *ResultType = ConvertType(TREE_TYPE(TREE_TYPE(fndecl)));
4887 std::vector<Value*> Operands;
4888 for (tree Op = TREE_OPERAND(exp, 1); Op; Op = TREE_CHAIN(Op)) {
4889 tree OpVal = TREE_VALUE(Op);
4890 if (isAggregateTreeType(TREE_TYPE(OpVal))) {
4891 MemRef OpLoc = CreateTempLoc(ConvertType(TREE_TYPE(OpVal)));
4892 Emit(OpVal, &OpLoc);
4893 Operands.push_back(Builder.CreateLoad(OpLoc.Ptr));
4894 } else {
4895 Operands.push_back(Emit(OpVal, NULL));
4899 unsigned FnCode = DECL_FUNCTION_CODE(fndecl);
4900 return LLVM_TARGET_INTRINSIC_LOWER(exp, FnCode, DestLoc, Result, ResultType,
4901 Operands);
4902 #endif
4903 return false;
4906 /// TargetBuiltinCache - A cache of builtin intrinsics indexed by the GCC
4907 /// builtin number.
4908 static std::vector<Constant*> TargetBuiltinCache;
4910 void clearTargetBuiltinCache() {
4911 TargetBuiltinCache.clear();
4914 void TreeToLLVM::EmitMemoryBarrier(bool ll, bool ls, bool sl, bool ss,
4915 bool device) {
4916 Value* C[5];
4917 C[0] = ConstantInt::get(Type::getInt1Ty(Context), ll);
4918 C[1] = ConstantInt::get(Type::getInt1Ty(Context), ls);
4919 C[2] = ConstantInt::get(Type::getInt1Ty(Context), sl);
4920 C[3] = ConstantInt::get(Type::getInt1Ty(Context), ss);
4921 C[4] = ConstantInt::get(Type::getInt1Ty(Context), device);
4923 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
4924 Intrinsic::memory_barrier),
4925 C, C + 5);
4928 Value *
4929 TreeToLLVM::BuildBinaryAtomicBuiltin(tree exp, Intrinsic::ID id) {
4930 const Type *ResultTy = ConvertType(TREE_TYPE(exp));
4931 tree arglist = TREE_OPERAND(exp, 1);
4932 Value* C[2] = {
4933 Emit(TREE_VALUE(arglist), 0),
4934 Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0)
4936 const Type* Ty[2];
4937 Ty[0] = ResultTy;
4938 Ty[1] = ResultTy->getPointerTo();
4939 C[0] = Builder.CreateBitCast(C[0], Ty[1]);
4940 C[1] = Builder.CreateIntCast(C[1], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
4941 "cast");
4942 // The gcc builtins are also full memory barriers.
4943 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
4944 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
4945 EmitMemoryBarrier(true, true, true, true, false);
4946 #else
4947 EmitMemoryBarrier(true, true, true, true, true);
4948 #endif
4950 Value *Result =
4951 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, id,
4952 Ty, 2),
4953 C, C + 2);
4955 // The gcc builtins are also full memory barriers.
4956 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
4957 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
4958 EmitMemoryBarrier(true, true, true, true, false);
4959 #else
4960 EmitMemoryBarrier(true, true, true, true, true);
4961 #endif
4963 Result = Builder.CreateIntToPtr(Result, ResultTy);
4964 return Result;
4967 Value *
4968 TreeToLLVM::BuildCmpAndSwapAtomicBuiltin(tree exp, tree type, bool isBool) {
4969 const Type *ResultTy = ConvertType(type);
4970 tree arglist = TREE_OPERAND(exp, 1);
4971 Value* C[3] = {
4972 Emit(TREE_VALUE(arglist), 0),
4973 Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0),
4974 Emit(TREE_VALUE(TREE_CHAIN(TREE_CHAIN(arglist))), 0)
4976 const Type* Ty[2];
4977 Ty[0] = ResultTy;
4978 Ty[1] = ResultTy->getPointerTo();
4979 C[0] = Builder.CreateBitCast(C[0], Ty[1]);
4980 C[1] = Builder.CreateIntCast(C[1], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
4981 "cast");
4982 C[2] = Builder.CreateIntCast(C[2], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
4983 "cast");
4985 // The gcc builtins are also full memory barriers.
4986 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
4987 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
4988 EmitMemoryBarrier(true, true, true, true, false);
4989 #else
4990 EmitMemoryBarrier(true, true, true, true, true);
4991 #endif
4993 Value *Result =
4994 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
4995 Intrinsic::atomic_cmp_swap,
4996 Ty, 2),
4997 C, C + 3);
4999 // The gcc builtins are also full memory barriers.
5000 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5001 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5002 EmitMemoryBarrier(true, true, true, true, false);
5003 #else
5004 EmitMemoryBarrier(true, true, true, true, true);
5005 #endif
5007 if (isBool)
5008 Result = CastToUIntType(Builder.CreateICmpEQ(Result, C[1]),
5009 ConvertType(boolean_type_node));
5010 else
5011 Result = Builder.CreateIntToPtr(Result, ResultTy);
5012 return Result;
5015 /// EmitBuiltinCall - exp is a call to fndecl, a builtin function. Try to emit
5016 /// the call in a special way, setting Result to the scalar result if necessary.
5017 /// If we can't handle the builtin, return false, otherwise return true.
5018 bool TreeToLLVM::EmitBuiltinCall(tree exp, tree fndecl,
5019 const MemRef *DestLoc, Value *&Result) {
5020 if (DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_MD) {
5021 unsigned FnCode = DECL_FUNCTION_CODE(fndecl);
5022 if (TargetBuiltinCache.size() <= FnCode)
5023 TargetBuiltinCache.resize(FnCode+1);
5025 // If we haven't converted this intrinsic over yet, do so now.
5026 if (TargetBuiltinCache[FnCode] == 0) {
5027 const char *TargetPrefix = "";
5028 #ifdef LLVM_TARGET_INTRINSIC_PREFIX
5029 TargetPrefix = LLVM_TARGET_INTRINSIC_PREFIX;
5030 #endif
5031 // If this builtin directly corresponds to an LLVM intrinsic, get the
5032 // IntrinsicID now.
5033 const char *BuiltinName = IDENTIFIER_POINTER(DECL_NAME(fndecl));
5034 Intrinsic::ID IntrinsicID =
5035 Intrinsic::getIntrinsicForGCCBuiltin(TargetPrefix, BuiltinName);
5036 if (IntrinsicID == Intrinsic::not_intrinsic) {
5037 if (EmitFrontendExpandedBuiltinCall(exp, fndecl, DestLoc, Result))
5038 return true;
5040 error("%Hunsupported target builtin %<%s%> used", &EXPR_LOCATION(exp),
5041 BuiltinName);
5042 const Type *ResTy = ConvertType(TREE_TYPE(exp));
5043 if (ResTy->isSingleValueType())
5044 Result = UndefValue::get(ResTy);
5045 return true;
5048 // Finally, map the intrinsic ID back to a name.
5049 TargetBuiltinCache[FnCode] =
5050 Intrinsic::getDeclaration(TheModule, IntrinsicID);
5053 Result = EmitCallOf(TargetBuiltinCache[FnCode], exp, DestLoc,
5054 AttrListPtr());
5055 return true;
5058 enum built_in_function fcode = DECL_FUNCTION_CODE(fndecl);
5059 switch (fcode) {
5060 default: return false;
5061 // Varargs builtins.
5062 case BUILT_IN_VA_START:
5063 case BUILT_IN_STDARG_START: return EmitBuiltinVAStart(exp);
5064 case BUILT_IN_VA_END: return EmitBuiltinVAEnd(exp);
5065 case BUILT_IN_VA_COPY: return EmitBuiltinVACopy(exp);
5066 case BUILT_IN_CONSTANT_P: return EmitBuiltinConstantP(exp, Result);
5067 case BUILT_IN_ALLOCA: return EmitBuiltinAlloca(exp, Result);
5068 case BUILT_IN_EXTEND_POINTER: return EmitBuiltinExtendPointer(exp, Result);
5069 case BUILT_IN_EXPECT: return EmitBuiltinExpect(exp, DestLoc, Result);
5070 case BUILT_IN_MEMCPY: return EmitBuiltinMemCopy(exp, Result,
5071 false, false);
5072 case BUILT_IN_MEMCPY_CHK: return EmitBuiltinMemCopy(exp, Result,
5073 false, true);
5074 case BUILT_IN_MEMMOVE: return EmitBuiltinMemCopy(exp, Result,
5075 true, false);
5076 case BUILT_IN_MEMMOVE_CHK: return EmitBuiltinMemCopy(exp, Result,
5077 true, true);
5078 case BUILT_IN_MEMSET: return EmitBuiltinMemSet(exp, Result, false);
5079 case BUILT_IN_MEMSET_CHK: return EmitBuiltinMemSet(exp, Result, true);
5080 case BUILT_IN_BZERO: return EmitBuiltinBZero(exp, Result);
5081 case BUILT_IN_PREFETCH: return EmitBuiltinPrefetch(exp);
5082 case BUILT_IN_FRAME_ADDRESS: return EmitBuiltinReturnAddr(exp, Result,true);
5083 case BUILT_IN_RETURN_ADDRESS: return EmitBuiltinReturnAddr(exp, Result,false);
5084 case BUILT_IN_STACK_SAVE: return EmitBuiltinStackSave(exp, Result);
5085 case BUILT_IN_STACK_RESTORE: return EmitBuiltinStackRestore(exp);
5086 case BUILT_IN_EXTRACT_RETURN_ADDR:
5087 return EmitBuiltinExtractReturnAddr(exp, Result);
5088 case BUILT_IN_FROB_RETURN_ADDR:
5089 return EmitBuiltinFrobReturnAddr(exp, Result);
5090 case BUILT_IN_INIT_TRAMPOLINE:
5091 return EmitBuiltinInitTrampoline(exp, Result);
5093 // Builtins used by the exception handling runtime.
5094 case BUILT_IN_DWARF_CFA:
5095 return EmitBuiltinDwarfCFA(exp, Result);
5096 #ifdef DWARF2_UNWIND_INFO
5097 case BUILT_IN_DWARF_SP_COLUMN:
5098 return EmitBuiltinDwarfSPColumn(exp, Result);
5099 case BUILT_IN_INIT_DWARF_REG_SIZES:
5100 return EmitBuiltinInitDwarfRegSizes(exp, Result);
5101 #endif
5102 case BUILT_IN_EH_RETURN:
5103 return EmitBuiltinEHReturn(exp, Result);
5104 #ifdef EH_RETURN_DATA_REGNO
5105 case BUILT_IN_EH_RETURN_DATA_REGNO:
5106 return EmitBuiltinEHReturnDataRegno(exp, Result);
5107 #endif
5108 case BUILT_IN_UNWIND_INIT:
5109 return EmitBuiltinUnwindInit(exp, Result);
5111 case BUILT_IN_OBJECT_SIZE: {
5112 tree arglist = TREE_OPERAND (exp, 1);
5113 if (!validate_arglist(arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) {
5114 error("Invalid builtin_object_size argument types");
5115 return false;
5117 tree ObjSizeTree = TREE_VALUE (TREE_CHAIN (arglist));
5118 STRIP_NOPS (ObjSizeTree);
5119 if (TREE_CODE (ObjSizeTree) != INTEGER_CST
5120 || tree_int_cst_sgn (ObjSizeTree) < 0
5121 || compare_tree_int (ObjSizeTree, 3) > 0) {
5122 error("Invalid second builtin_object_size argument");
5123 return false;
5126 tree Object = TREE_VALUE(arglist);
5127 tree ObjTy = TREE_VALUE(TREE_CHAIN(arglist));
5129 // LLVM doesn't handle type 1 or type 3. Deal with that here.
5130 Value *Tmp = Emit(ObjTy, 0);
5132 ConstantInt *CI = cast<ConstantInt>(Tmp);
5134 // Clear the bottom bit since we only handle whole objects and shift to turn
5135 // the second bit into our boolean.
5136 uint64_t val = (CI->getZExtValue() & 0x2) >> 1;
5138 Value *NewTy = ConstantInt::get(Tmp->getType(), val);
5140 Value* Args[] = {
5141 Emit(Object, 0),
5142 NewTy
5145 // Grab the current return type.
5146 const Type* Ty;
5147 Ty = ConvertType(TREE_TYPE(exp));
5149 // Manually coerce the arg to the correct pointer type.
5150 Args[0] = Builder.CreateBitCast(Args[0], Type::getInt8PtrTy(Context));
5151 Args[1] = Builder.CreateIntCast(Args[1], Type::getInt1Ty(Context),
5152 false);
5154 Result = Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5155 Intrinsic::objectsize,
5156 &Ty,
5158 Args, Args + 2);
5159 return true;
5161 // Unary bit counting intrinsics.
5162 // NOTE: do not merge these case statements. That will cause the memoized
5163 // Function* to be incorrectly shared across the different typed functions.
5164 case BUILT_IN_CLZ: // These GCC builtins always return int.
5165 case BUILT_IN_CLZL:
5166 case BUILT_IN_CLZLL: {
5167 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5168 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::ctlz);
5169 const Type *DestTy = ConvertType(TREE_TYPE(exp));
5170 Result = Builder.CreateIntCast(Result, DestTy, !TYPE_UNSIGNED(TREE_TYPE(exp)),
5171 "cast");
5172 return true;
5174 case BUILT_IN_CTZ: // These GCC builtins always return int.
5175 case BUILT_IN_CTZL:
5176 case BUILT_IN_CTZLL: {
5177 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5178 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::cttz);
5179 const Type *DestTy = ConvertType(TREE_TYPE(exp));
5180 Result = Builder.CreateIntCast(Result, DestTy, !TYPE_UNSIGNED(TREE_TYPE(exp)),
5181 "cast");
5182 return true;
5184 case BUILT_IN_PARITYLL:
5185 case BUILT_IN_PARITYL:
5186 case BUILT_IN_PARITY: {
5187 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5188 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::ctpop);
5189 Result = Builder.CreateBinOp(Instruction::And, Result,
5190 ConstantInt::get(Result->getType(), 1));
5191 return true;
5193 case BUILT_IN_POPCOUNT: // These GCC builtins always return int.
5194 case BUILT_IN_POPCOUNTL:
5195 case BUILT_IN_POPCOUNTLL: {
5196 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5197 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::ctpop);
5198 const Type *DestTy = ConvertType(TREE_TYPE(exp));
5199 Result = Builder.CreateIntCast(Result, DestTy, !TYPE_UNSIGNED(TREE_TYPE(exp)),
5200 "cast");
5201 return true;
5203 case BUILT_IN_BSWAP32:
5204 case BUILT_IN_BSWAP64: {
5205 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5206 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::bswap);
5207 const Type *DestTy = ConvertType(TREE_TYPE(exp));
5208 Result = Builder.CreateIntCast(Result, DestTy, !TYPE_UNSIGNED(TREE_TYPE(exp)),
5209 "cast");
5210 return true;
5213 case BUILT_IN_SQRT:
5214 case BUILT_IN_SQRTF:
5215 case BUILT_IN_SQRTL:
5216 // The result of sqrt(negative) is implementation-defined, but follows
5217 // IEEE754 in most current implementations. llvm.sqrt, which has undefined
5218 // behavior for such inputs, is an inappropriate substitute.
5219 break;
5220 case BUILT_IN_POWI:
5221 case BUILT_IN_POWIF:
5222 case BUILT_IN_POWIL:
5223 Result = EmitBuiltinPOWI(exp);
5224 return true;
5225 case BUILT_IN_POW:
5226 case BUILT_IN_POWF:
5227 case BUILT_IN_POWL:
5228 // If errno math has been disabled, expand these to llvm.pow calls.
5229 if (!flag_errno_math) {
5230 Result = EmitBuiltinPOW(exp);
5231 return true;
5233 break;
5234 case BUILT_IN_LOG:
5235 case BUILT_IN_LOGF:
5236 case BUILT_IN_LOGL:
5237 // If errno math has been disabled, expand these to llvm.log calls.
5238 if (!flag_errno_math) {
5239 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5240 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::log);
5241 Result = CastToFPType(Result, ConvertType(TREE_TYPE(exp)));
5242 return true;
5244 break;
5245 case BUILT_IN_LOG2:
5246 case BUILT_IN_LOG2F:
5247 case BUILT_IN_LOG2L:
5248 // If errno math has been disabled, expand these to llvm.log2 calls.
5249 if (!flag_errno_math) {
5250 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5251 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::log2);
5252 Result = CastToFPType(Result, ConvertType(TREE_TYPE(exp)));
5253 return true;
5255 break;
5256 case BUILT_IN_LOG10:
5257 case BUILT_IN_LOG10F:
5258 case BUILT_IN_LOG10L:
5259 // If errno math has been disabled, expand these to llvm.log10 calls.
5260 if (!flag_errno_math) {
5261 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5262 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::log10);
5263 Result = CastToFPType(Result, ConvertType(TREE_TYPE(exp)));
5264 return true;
5266 break;
5267 case BUILT_IN_EXP:
5268 case BUILT_IN_EXPF:
5269 case BUILT_IN_EXPL:
5270 // If errno math has been disabled, expand these to llvm.exp calls.
5271 if (!flag_errno_math) {
5272 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5273 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::exp);
5274 Result = CastToFPType(Result, ConvertType(TREE_TYPE(exp)));
5275 return true;
5277 break;
5278 case BUILT_IN_EXP2:
5279 case BUILT_IN_EXP2F:
5280 case BUILT_IN_EXP2L:
5281 // If errno math has been disabled, expand these to llvm.exp2 calls.
5282 if (!flag_errno_math) {
5283 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5284 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::exp2);
5285 Result = CastToFPType(Result, ConvertType(TREE_TYPE(exp)));
5286 return true;
5288 break;
5289 case BUILT_IN_FFS: // These GCC builtins always return int.
5290 case BUILT_IN_FFSL:
5291 case BUILT_IN_FFSLL: { // FFS(X) -> (x == 0 ? 0 : CTTZ(x)+1)
5292 // The argument and return type of cttz should match the argument type of
5293 // the ffs, but should ignore the return type of ffs.
5294 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5295 EmitBuiltinUnaryOp(Amt, Result, Intrinsic::cttz);
5296 Result = Builder.CreateAdd(Result,
5297 ConstantInt::get(Result->getType(), 1));
5298 Result = CastToUIntType(Result, ConvertType(TREE_TYPE(exp)));
5299 Value *Cond =
5300 Builder.CreateICmpEQ(Amt,
5301 Constant::getNullValue(Amt->getType()));
5302 Result = Builder.CreateSelect(Cond,
5303 Constant::getNullValue(Result->getType()),
5304 Result);
5305 return true;
5307 case BUILT_IN_FLT_ROUNDS: {
5308 Result =
5309 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5310 Intrinsic::flt_rounds));
5311 Result = BitCastToType(Result, ConvertType(TREE_TYPE(exp)));
5312 return true;
5314 case BUILT_IN_TRAP:
5315 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, Intrinsic::trap));
5316 // Emit an explicit unreachable instruction.
5317 Builder.CreateUnreachable();
5318 EmitBlock(BasicBlock::Create(Context, ""));
5319 return true;
5321 // Convert annotation built-in to llvm.annotation intrinsic.
5322 case BUILT_IN_ANNOTATION: {
5324 // Get file and line number
5325 location_t locus = EXPR_LOCATION (exp);
5326 Constant *lineNo = ConstantInt::get(Type::getInt32Ty(Context), locus.line);
5327 Constant *file = ConvertMetadataStringToGV(locus.file);
5328 const Type *SBP= Type::getInt8PtrTy(Context);
5329 file = Builder.getFolder().CreateBitCast(file, SBP);
5331 // Get arguments.
5332 tree arglist = TREE_OPERAND(exp, 1);
5333 Value *ExprVal = Emit(TREE_VALUE(arglist), 0);
5334 const Type *Ty = ExprVal->getType();
5335 Value *StrVal = Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0);
5337 SmallVector<Value *, 4> Args;
5338 Args.push_back(ExprVal);
5339 Args.push_back(StrVal);
5340 Args.push_back(file);
5341 Args.push_back(lineNo);
5343 assert(Ty && "llvm.annotation arg type may not be null");
5344 Result = Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5345 Intrinsic::annotation,
5346 &Ty,
5348 Args.begin(), Args.end());
5349 return true;
5352 case BUILT_IN_SYNCHRONIZE: {
5353 #if defined(TARGET_ARM)
5354 if (TARGET_THUMB1 || !arm_arch6)
5355 return false;
5356 #endif
5357 // We assume like gcc appears to, that this only applies to cached memory.
5358 Value* C[5];
5359 C[0] = C[1] = C[2] = C[3] = ConstantInt::get(Type::getInt1Ty(Context), 1);
5360 C[4] = ConstantInt::get(Type::getInt1Ty(Context), 0);
5362 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5363 Intrinsic::memory_barrier),
5364 C, C + 5);
5365 return true;
5367 #if defined(TARGET_ALPHA) || defined(TARGET_386) || defined(TARGET_POWERPC) \
5368 || defined(TARGET_ARM)
5369 // gcc uses many names for the sync intrinsics
5370 // The type of the first argument is not reliable for choosing the
5371 // right llvm function; if the original type is not volatile, gcc has
5372 // helpfully changed it to "volatile void *" at this point. The
5373 // original type can be recovered from the function type in most cases.
5374 // For lock_release and bool_compare_and_swap even that is not good
5375 // enough, we have to key off the opcode.
5376 // Note that Intrinsic::getDeclaration expects the type list in reversed
5377 // order, while CreateCall expects the parameter list in normal order.
5378 case BUILT_IN_BOOL_COMPARE_AND_SWAP_1: {
5379 #if defined(TARGET_ARM)
5380 if (TARGET_THUMB1 || !arm_arch6)
5381 return false;
5382 #endif
5383 Result = BuildCmpAndSwapAtomicBuiltin(exp, unsigned_char_type_node, true);
5384 return true;
5386 case BUILT_IN_BOOL_COMPARE_AND_SWAP_2: {
5387 #if defined(TARGET_ARM)
5388 if (TARGET_THUMB1 || !arm_arch6)
5389 return false;
5390 #endif
5391 Result = BuildCmpAndSwapAtomicBuiltin(exp, short_unsigned_type_node, true);
5392 return true;
5394 case BUILT_IN_BOOL_COMPARE_AND_SWAP_4: {
5395 #if defined(TARGET_ARM)
5396 if (TARGET_THUMB1 || !arm_arch6)
5397 return false;
5398 #endif
5399 Result = BuildCmpAndSwapAtomicBuiltin(exp, unsigned_type_node, true);
5400 return true;
5402 case BUILT_IN_BOOL_COMPARE_AND_SWAP_8: {
5403 #if defined(TARGET_ARM)
5404 return false;
5405 #endif
5406 #if defined(TARGET_POWERPC)
5407 if (!TARGET_64BIT)
5408 return false;
5409 #endif
5410 Result = BuildCmpAndSwapAtomicBuiltin(exp, long_long_unsigned_type_node,
5411 true);
5412 return true;
5415 case BUILT_IN_VAL_COMPARE_AND_SWAP_8:
5416 #if defined(TARGET_ARM)
5417 return false;
5418 #endif
5419 #if defined(TARGET_POWERPC)
5420 if (!TARGET_64BIT)
5421 return false;
5422 #endif
5423 case BUILT_IN_VAL_COMPARE_AND_SWAP_1:
5424 case BUILT_IN_VAL_COMPARE_AND_SWAP_2:
5425 case BUILT_IN_VAL_COMPARE_AND_SWAP_4: {
5426 #if defined(TARGET_ARM)
5427 if (TARGET_THUMB1 || !arm_arch6)
5428 return false;
5429 #endif
5430 tree type = TREE_TYPE(exp);
5431 Result = BuildCmpAndSwapAtomicBuiltin(exp, type, false);
5432 return true;
5434 case BUILT_IN_FETCH_AND_ADD_8:
5435 #if defined(TARGET_ARM)
5436 return false;
5437 #endif
5438 #if defined(TARGET_POWERPC)
5439 if (!TARGET_64BIT)
5440 return false;
5441 #endif
5442 case BUILT_IN_FETCH_AND_ADD_1:
5443 case BUILT_IN_FETCH_AND_ADD_2:
5444 case BUILT_IN_FETCH_AND_ADD_4: {
5445 #if defined(TARGET_ARM)
5446 if (TARGET_THUMB1 || !arm_arch6)
5447 return false;
5448 #endif
5449 Result = BuildBinaryAtomicBuiltin(exp, Intrinsic::atomic_load_add);
5450 return true;
5452 case BUILT_IN_FETCH_AND_SUB_8:
5453 #if defined(TARGET_ARM)
5454 return false;
5455 #endif
5456 #if defined(TARGET_POWERPC)
5457 if (!TARGET_64BIT)
5458 return false;
5459 #endif
5460 case BUILT_IN_FETCH_AND_SUB_1:
5461 case BUILT_IN_FETCH_AND_SUB_2:
5462 case BUILT_IN_FETCH_AND_SUB_4: {
5463 #if defined(TARGET_ARM)
5464 if (TARGET_THUMB1 || !arm_arch6)
5465 return false;
5466 #endif
5467 Result = BuildBinaryAtomicBuiltin(exp, Intrinsic::atomic_load_sub);
5468 return true;
5470 case BUILT_IN_FETCH_AND_OR_8:
5471 #if defined(TARGET_ARM)
5472 return false;
5473 #endif
5474 #if defined(TARGET_POWERPC)
5475 if (!TARGET_64BIT)
5476 return false;
5477 #endif
5478 case BUILT_IN_FETCH_AND_OR_1:
5479 case BUILT_IN_FETCH_AND_OR_2:
5480 case BUILT_IN_FETCH_AND_OR_4: {
5481 #if defined(TARGET_ARM)
5482 if (TARGET_THUMB1 || !arm_arch6)
5483 return false;
5484 #endif
5485 Result = BuildBinaryAtomicBuiltin(exp, Intrinsic::atomic_load_or);
5486 return true;
5488 case BUILT_IN_FETCH_AND_AND_8:
5489 #if defined(TARGET_ARM)
5490 return false;
5491 #endif
5492 #if defined(TARGET_POWERPC)
5493 if (!TARGET_64BIT)
5494 return false;
5495 #endif
5496 case BUILT_IN_FETCH_AND_AND_1:
5497 case BUILT_IN_FETCH_AND_AND_2:
5498 case BUILT_IN_FETCH_AND_AND_4: {
5499 #if defined(TARGET_ARM)
5500 if (TARGET_THUMB1 || !arm_arch6)
5501 return false;
5502 #endif
5503 Result = BuildBinaryAtomicBuiltin(exp, Intrinsic::atomic_load_and);
5504 return true;
5506 case BUILT_IN_FETCH_AND_XOR_8:
5507 #if defined(TARGET_ARM)
5508 return false;
5509 #endif
5510 #if defined(TARGET_POWERPC)
5511 if (!TARGET_64BIT)
5512 return false;
5513 #endif
5514 case BUILT_IN_FETCH_AND_XOR_1:
5515 case BUILT_IN_FETCH_AND_XOR_2:
5516 case BUILT_IN_FETCH_AND_XOR_4: {
5517 #if defined(TARGET_ARM)
5518 if (TARGET_THUMB1 || !arm_arch6)
5519 return false;
5520 #endif
5521 Result = BuildBinaryAtomicBuiltin(exp, Intrinsic::atomic_load_xor);
5522 return true;
5524 case BUILT_IN_FETCH_AND_NAND_8:
5525 #if defined(TARGET_ARM)
5526 return false;
5527 #endif
5528 #if defined(TARGET_POWERPC)
5529 if (!TARGET_64BIT)
5530 return false;
5531 #endif
5532 case BUILT_IN_FETCH_AND_NAND_1:
5533 case BUILT_IN_FETCH_AND_NAND_2:
5534 case BUILT_IN_FETCH_AND_NAND_4: {
5535 #if defined(TARGET_ARM)
5536 if (TARGET_THUMB1 || !arm_arch6)
5537 return false;
5538 #endif
5539 Result = BuildBinaryAtomicBuiltin(exp, Intrinsic::atomic_load_nand);
5540 return true;
5542 case BUILT_IN_LOCK_TEST_AND_SET_8:
5543 #if defined(TARGET_ARM)
5544 return false;
5545 #endif
5546 #if defined(TARGET_POWERPC)
5547 if (!TARGET_64BIT)
5548 return false;
5549 #endif
5550 case BUILT_IN_LOCK_TEST_AND_SET_1:
5551 case BUILT_IN_LOCK_TEST_AND_SET_2:
5552 case BUILT_IN_LOCK_TEST_AND_SET_4: {
5553 #if defined(TARGET_ARM)
5554 if (TARGET_THUMB1 || !arm_arch6)
5555 return false;
5556 #endif
5557 Result = BuildBinaryAtomicBuiltin(exp, Intrinsic::atomic_swap);
5558 return true;
5561 case BUILT_IN_ADD_AND_FETCH_8:
5562 #if defined(TARGET_ARM)
5563 return false;
5564 #endif
5565 #if defined(TARGET_POWERPC)
5566 if (!TARGET_64BIT)
5567 return false;
5568 #endif
5569 case BUILT_IN_ADD_AND_FETCH_1:
5570 case BUILT_IN_ADD_AND_FETCH_2:
5571 case BUILT_IN_ADD_AND_FETCH_4: {
5572 #if defined(TARGET_ARM)
5573 if (TARGET_THUMB1 || !arm_arch6)
5574 return false;
5575 #endif
5576 const Type *ResultTy = ConvertType(TREE_TYPE(exp));
5577 tree arglist = TREE_OPERAND(exp, 1);
5578 Value* C[2] = {
5579 Emit(TREE_VALUE(arglist), 0),
5580 Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0)
5582 const Type* Ty[2];
5583 Ty[0] = ResultTy;
5584 Ty[1] = ResultTy->getPointerTo();
5585 C[0] = Builder.CreateBitCast(C[0], Ty[1]);
5586 C[1] = Builder.CreateIntCast(C[1], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
5587 "cast");
5589 // The gcc builtins are also full memory barriers.
5590 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5591 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5592 EmitMemoryBarrier(true, true, true, true, false);
5593 #else
5594 EmitMemoryBarrier(true, true, true, true, true);
5595 #endif
5597 Result =
5598 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5599 Intrinsic::atomic_load_add,
5600 Ty, 2),
5601 C, C + 2);
5603 // The gcc builtins are also full memory barriers.
5604 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5605 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5606 EmitMemoryBarrier(true, true, true, true, false);
5607 #else
5608 EmitMemoryBarrier(true, true, true, true, true);
5609 #endif
5611 Result = Builder.CreateAdd(Result, C[1]);
5612 Result = Builder.CreateIntToPtr(Result, ResultTy);
5613 return true;
5615 case BUILT_IN_SUB_AND_FETCH_8:
5616 #if defined(TARGET_ARM)
5617 return false;
5618 #endif
5619 #if defined(TARGET_POWERPC)
5620 if (!TARGET_64BIT)
5621 return false;
5622 #endif
5623 case BUILT_IN_SUB_AND_FETCH_1:
5624 case BUILT_IN_SUB_AND_FETCH_2:
5625 case BUILT_IN_SUB_AND_FETCH_4: {
5626 #if defined(TARGET_ARM)
5627 if (TARGET_THUMB1 || !arm_arch6)
5628 return false;
5629 #endif
5630 const Type *ResultTy = ConvertType(TREE_TYPE(exp));
5631 tree arglist = TREE_OPERAND(exp, 1);
5632 Value* C[2] = {
5633 Emit(TREE_VALUE(arglist), 0),
5634 Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0)
5636 const Type* Ty[2];
5637 Ty[0] = ResultTy;
5638 Ty[1] = ResultTy->getPointerTo();
5639 C[0] = Builder.CreateBitCast(C[0], Ty[1]);
5640 C[1] = Builder.CreateIntCast(C[1], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
5641 "cast");
5643 // The gcc builtins are also full memory barriers.
5644 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5645 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5646 EmitMemoryBarrier(true, true, true, true, false);
5647 #else
5648 EmitMemoryBarrier(true, true, true, true, true);
5649 #endif
5651 Result =
5652 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5653 Intrinsic::atomic_load_sub,
5654 Ty, 2),
5655 C, C + 2);
5657 // The gcc builtins are also full memory barriers.
5658 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5659 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5660 EmitMemoryBarrier(true, true, true, true, false);
5661 #else
5662 EmitMemoryBarrier(true, true, true, true, true);
5663 #endif
5665 Result = Builder.CreateSub(Result, C[1]);
5666 Result = Builder.CreateIntToPtr(Result, ResultTy);
5667 return true;
5669 case BUILT_IN_OR_AND_FETCH_8:
5670 #if defined(TARGET_ARM)
5671 return false;
5672 #endif
5673 #if defined(TARGET_POWERPC)
5674 if (!TARGET_64BIT)
5675 return false;
5676 #endif
5677 case BUILT_IN_OR_AND_FETCH_1:
5678 case BUILT_IN_OR_AND_FETCH_2:
5679 case BUILT_IN_OR_AND_FETCH_4: {
5680 #if defined(TARGET_ARM)
5681 if (TARGET_THUMB1 || !arm_arch6)
5682 return false;
5683 #endif
5684 const Type *ResultTy = ConvertType(TREE_TYPE(exp));
5685 tree arglist = TREE_OPERAND(exp, 1);
5686 Value* C[2] = {
5687 Emit(TREE_VALUE(arglist), 0),
5688 Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0)
5690 const Type* Ty[2];
5691 Ty[0] = ResultTy;
5692 Ty[1] = ResultTy->getPointerTo();
5693 C[0] = Builder.CreateBitCast(C[0], Ty[1]);
5694 C[1] = Builder.CreateIntCast(C[1], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
5695 "cast");
5697 // The gcc builtins are also full memory barriers.
5698 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5699 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5700 EmitMemoryBarrier(true, true, true, true, false);
5701 #else
5702 EmitMemoryBarrier(true, true, true, true, true);
5703 #endif
5705 Result =
5706 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5707 Intrinsic::atomic_load_or,
5708 Ty, 2),
5709 C, C + 2);
5711 // The gcc builtins are also full memory barriers.
5712 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5713 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5714 EmitMemoryBarrier(true, true, true, true, false);
5715 #else
5716 EmitMemoryBarrier(true, true, true, true, true);
5717 #endif
5719 Result = Builder.CreateOr(Result, C[1]);
5720 Result = Builder.CreateIntToPtr(Result, ResultTy);
5721 return true;
5723 case BUILT_IN_AND_AND_FETCH_8:
5724 #if defined(TARGET_ARM)
5725 return false;
5726 #endif
5727 #if defined(TARGET_POWERPC)
5728 if (!TARGET_64BIT)
5729 return false;
5730 #endif
5731 case BUILT_IN_AND_AND_FETCH_1:
5732 case BUILT_IN_AND_AND_FETCH_2:
5733 case BUILT_IN_AND_AND_FETCH_4: {
5734 #if defined(TARGET_ARM)
5735 if (TARGET_THUMB1 || !arm_arch6)
5736 return false;
5737 #endif
5738 const Type *ResultTy = ConvertType(TREE_TYPE(exp));
5739 tree arglist = TREE_OPERAND(exp, 1);
5740 Value* C[2] = {
5741 Emit(TREE_VALUE(arglist), 0),
5742 Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0)
5744 const Type* Ty[2];
5745 Ty[0] = ResultTy;
5746 Ty[1] = ResultTy->getPointerTo();
5747 C[0] = Builder.CreateBitCast(C[0], Ty[1]);
5748 C[1] = Builder.CreateIntCast(C[1], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
5749 "cast");
5751 // The gcc builtins are also full memory barriers.
5752 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5753 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5754 EmitMemoryBarrier(true, true, true, true, false);
5755 #else
5756 EmitMemoryBarrier(true, true, true, true, true);
5757 #endif
5759 Result =
5760 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5761 Intrinsic::atomic_load_and,
5762 Ty, 2),
5763 C, C + 2);
5765 // The gcc builtins are also full memory barriers.
5766 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5767 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5768 EmitMemoryBarrier(true, true, true, true, false);
5769 #else
5770 EmitMemoryBarrier(true, true, true, true, true);
5771 #endif
5773 Result = Builder.CreateAnd(Result, C[1]);
5774 Result = Builder.CreateIntToPtr(Result, ResultTy);
5775 return true;
5777 case BUILT_IN_XOR_AND_FETCH_8:
5778 #if defined(TARGET_ARM)
5779 return false;
5780 #endif
5781 #if defined(TARGET_POWERPC)
5782 if (!TARGET_64BIT)
5783 return false;
5784 #endif
5785 case BUILT_IN_XOR_AND_FETCH_1:
5786 case BUILT_IN_XOR_AND_FETCH_2:
5787 case BUILT_IN_XOR_AND_FETCH_4: {
5788 #if defined(TARGET_ARM)
5789 if (TARGET_THUMB1 || !arm_arch6)
5790 return false;
5791 #endif
5792 const Type *ResultTy = ConvertType(TREE_TYPE(exp));
5793 tree arglist = TREE_OPERAND(exp, 1);
5794 Value* C[2] = {
5795 Emit(TREE_VALUE(arglist), 0),
5796 Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0)
5798 const Type* Ty[2];
5799 Ty[0] = ResultTy;
5800 Ty[1] = ResultTy->getPointerTo();
5801 C[0] = Builder.CreateBitCast(C[0], Ty[1]);
5802 C[1] = Builder.CreateIntCast(C[1], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
5803 "cast");
5805 // The gcc builtins are also full memory barriers.
5806 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5807 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5808 EmitMemoryBarrier(true, true, true, true, false);
5809 #else
5810 EmitMemoryBarrier(true, true, true, true, true);
5811 #endif
5813 Result =
5814 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5815 Intrinsic::atomic_load_xor,
5816 Ty, 2),
5817 C, C + 2);
5819 // The gcc builtins are also full memory barriers.
5820 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5821 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5822 EmitMemoryBarrier(true, true, true, true, false);
5823 #else
5824 EmitMemoryBarrier(true, true, true, true, true);
5825 #endif
5827 Result = Builder.CreateXor(Result, C[1]);
5828 Result = Builder.CreateIntToPtr(Result, ResultTy);
5829 return true;
5831 case BUILT_IN_NAND_AND_FETCH_8:
5832 #if defined(TARGET_ARM)
5833 return false;
5834 #endif
5835 #if defined(TARGET_POWERPC)
5836 if (!TARGET_64BIT)
5837 return false;
5838 #endif
5839 case BUILT_IN_NAND_AND_FETCH_1:
5840 case BUILT_IN_NAND_AND_FETCH_2:
5841 case BUILT_IN_NAND_AND_FETCH_4: {
5842 #if defined(TARGET_ARM)
5843 if (TARGET_THUMB1 || !arm_arch6)
5844 return false;
5845 #endif
5846 const Type *ResultTy = ConvertType(TREE_TYPE(exp));
5847 tree arglist = TREE_OPERAND(exp, 1);
5848 Value* C[2] = {
5849 Emit(TREE_VALUE(arglist), 0),
5850 Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0)
5852 const Type* Ty[2];
5853 Ty[0] = ResultTy;
5854 Ty[1] = ResultTy->getPointerTo();
5855 C[0] = Builder.CreateBitCast(C[0], Ty[1]);
5856 C[1] = Builder.CreateIntCast(C[1], Ty[0], !TYPE_UNSIGNED(TREE_TYPE(exp)),
5857 "cast");
5859 // The gcc builtins are also full memory barriers.
5860 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5861 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5862 EmitMemoryBarrier(true, true, true, true, false);
5863 #else
5864 EmitMemoryBarrier(true, true, true, true, true);
5865 #endif
5867 Result =
5868 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5869 Intrinsic::atomic_load_nand,
5870 Ty, 2),
5871 C, C + 2);
5873 // The gcc builtins are also full memory barriers.
5874 // FIXME: __sync_lock_test_and_set and __sync_lock_release require less.
5875 #if defined(TARGET_ARM) && defined(CONFIG_DARWIN_H)
5876 EmitMemoryBarrier(true, true, true, true, false);
5877 #else
5878 EmitMemoryBarrier(true, true, true, true, true);
5879 #endif
5881 Result = Builder.CreateAnd(Builder.CreateNot(Result), C[1]);
5882 Result = Builder.CreateIntToPtr(Result, ResultTy);
5883 return true;
5886 case BUILT_IN_LOCK_RELEASE_1:
5887 case BUILT_IN_LOCK_RELEASE_2:
5888 case BUILT_IN_LOCK_RELEASE_4:
5889 case BUILT_IN_LOCK_RELEASE_8:
5890 case BUILT_IN_LOCK_RELEASE_16: {
5891 // This is effectively a volatile store of 0, and has no return value.
5892 // The argument has typically been coerced to "volatile void*"; the
5893 // only way to find the size of the operation is from the builtin
5894 // opcode.
5895 tree type;
5896 switch(DECL_FUNCTION_CODE(fndecl)) {
5897 case BUILT_IN_LOCK_RELEASE_1:
5898 type = unsigned_char_type_node; break;
5899 case BUILT_IN_LOCK_RELEASE_2:
5900 type = short_unsigned_type_node; break;
5901 case BUILT_IN_LOCK_RELEASE_4:
5902 type = unsigned_type_node; break;
5903 case BUILT_IN_LOCK_RELEASE_8:
5904 type = long_long_unsigned_type_node; break;
5905 case BUILT_IN_LOCK_RELEASE_16: // not handled; should use SSE on x86
5906 default:
5907 abort();
5909 tree arglist = TREE_OPERAND(exp, 1);
5910 tree t1 = build1 (INDIRECT_REF, type, TREE_VALUE (arglist));
5911 TREE_THIS_VOLATILE(t1) = 1;
5912 tree t = build2 (MODIFY_EXPR, type, t1,
5913 build_int_cst (type, (HOST_WIDE_INT)0));
5914 EmitMODIFY_EXPR(t, 0);
5915 Result = 0;
5916 return true;
5919 #endif //FIXME: these break the build for backends that haven't implemented them
5922 #if 1 // FIXME: Should handle these GCC extensions eventually.
5923 case BUILT_IN_LONGJMP: {
5924 tree arglist = TREE_OPERAND(exp, 1);
5926 if (validate_arglist(arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE)) {
5927 tree value = TREE_VALUE(TREE_CHAIN(arglist));
5929 if (TREE_CODE(value) != INTEGER_CST ||
5930 cast<ConstantInt>(Emit(value, 0))->getValue() != 1) {
5931 error ("%<__builtin_longjmp%> second argument must be 1");
5932 return false;
5936 case BUILT_IN_APPLY_ARGS:
5937 case BUILT_IN_APPLY:
5938 case BUILT_IN_RETURN:
5939 case BUILT_IN_SAVEREGS:
5940 case BUILT_IN_ARGS_INFO:
5941 case BUILT_IN_NEXT_ARG:
5942 case BUILT_IN_CLASSIFY_TYPE:
5943 case BUILT_IN_AGGREGATE_INCOMING_ADDRESS:
5944 case BUILT_IN_SETJMP_SETUP:
5945 case BUILT_IN_SETJMP_DISPATCHER:
5946 case BUILT_IN_SETJMP_RECEIVER:
5947 case BUILT_IN_UPDATE_SETJMP_BUF:
5949 // FIXME: HACK: Just ignore these.
5951 const Type *Ty = ConvertType(TREE_TYPE(exp));
5952 if (Ty != Type::getVoidTy(Context))
5953 Result = Constant::getNullValue(Ty);
5954 return true;
5956 #endif // FIXME: Should handle these GCC extensions eventually.
5958 return false;
5961 bool TreeToLLVM::EmitBuiltinUnaryOp(Value *InVal, Value *&Result,
5962 Intrinsic::ID Id) {
5963 // The intrinsic might be overloaded in which case the argument is of
5964 // varying type. Make sure that we specify the actual type for "iAny"
5965 // by passing it as the 3rd and 4th parameters. This isn't needed for
5966 // most intrinsics, but is needed for ctpop, cttz, ctlz.
5967 const Type *Ty = InVal->getType();
5968 Result = Builder.CreateCall(Intrinsic::getDeclaration(TheModule, Id, &Ty, 1),
5969 InVal);
5970 return true;
5973 Value *TreeToLLVM::EmitBuiltinSQRT(tree exp) {
5974 Value *Amt = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
5975 const Type* Ty = Amt->getType();
5977 return Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5978 Intrinsic::sqrt, &Ty, 1),
5979 Amt);
5982 Value *TreeToLLVM::EmitBuiltinPOWI(tree exp) {
5983 tree ArgList = TREE_OPERAND (exp, 1);
5984 if (!validate_arglist(ArgList, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
5985 return 0;
5987 Value *Val = Emit(TREE_VALUE(ArgList), 0);
5988 Value *Pow = Emit(TREE_VALUE(TREE_CHAIN(ArgList)), 0);
5989 const Type *Ty = Val->getType();
5990 Pow = CastToSIntType(Pow, Type::getInt32Ty(Context));
5992 SmallVector<Value *,2> Args;
5993 Args.push_back(Val);
5994 Args.push_back(Pow);
5995 return Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
5996 Intrinsic::powi, &Ty, 1),
5997 Args.begin(), Args.end());
6000 Value *TreeToLLVM::EmitBuiltinPOW(tree exp) {
6001 tree ArgList = TREE_OPERAND (exp, 1);
6002 if (!validate_arglist(ArgList, REAL_TYPE, REAL_TYPE, VOID_TYPE))
6003 return 0;
6005 Value *Val = Emit(TREE_VALUE(ArgList), 0);
6006 Value *Pow = Emit(TREE_VALUE(TREE_CHAIN(ArgList)), 0);
6007 const Type *Ty = Val->getType();
6009 SmallVector<Value *,2> Args;
6010 Args.push_back(Val);
6011 Args.push_back(Pow);
6012 return Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
6013 Intrinsic::pow, &Ty, 1),
6014 Args.begin(), Args.end());
6017 bool TreeToLLVM::EmitBuiltinConstantP(tree exp, Value *&Result) {
6018 Result = Constant::getNullValue(ConvertType(TREE_TYPE(exp)));
6019 return true;
6022 bool TreeToLLVM::EmitBuiltinExtendPointer(tree exp, Value *&Result) {
6023 tree arglist = TREE_OPERAND(exp, 1);
6024 Value *Amt = Emit(TREE_VALUE(arglist), 0);
6025 bool AmtIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_VALUE(arglist)));
6026 bool ExpIsSigned = !TYPE_UNSIGNED(TREE_TYPE(exp));
6027 Result = CastToAnyType(Amt, AmtIsSigned, ConvertType(TREE_TYPE(exp)),
6028 ExpIsSigned);
6029 return true;
6032 /// OptimizeIntoPlainBuiltIn - Return true if it's safe to lower the object
6033 /// size checking builtin calls (e.g. __builtin___memcpy_chk into the
6034 /// plain non-checking calls. If the size of the argument is either -1 (unknown)
6035 /// or large enough to ensure no overflow (> len), then it's safe to do so.
6036 static bool OptimizeIntoPlainBuiltIn(tree exp, Value *Len, Value *Size) {
6037 if (BitCastInst *SizeBC = dyn_cast<BitCastInst>(Size))
6038 Size = SizeBC->getOperand(0);
6039 ConstantInt *SizeCI = dyn_cast<ConstantInt>(Size);
6040 if (!SizeCI)
6041 return false;
6042 if (SizeCI->isAllOnesValue())
6043 // If size is -1, convert to plain memcpy, etc.
6044 return true;
6046 if (BitCastInst *LenBC = dyn_cast<BitCastInst>(Len))
6047 Len = LenBC->getOperand(0);
6048 ConstantInt *LenCI = dyn_cast<ConstantInt>(Len);
6049 if (!LenCI)
6050 return false;
6051 if (SizeCI->getValue().ult(LenCI->getValue())) {
6052 location_t locus = EXPR_LOCATION(exp);
6053 warning (0, "%Hcall to %D will always overflow destination buffer",
6054 &locus, get_callee_fndecl(exp));
6055 return false;
6057 return true;
6060 /// EmitBuiltinMemCopy - Emit an llvm.memcpy or llvm.memmove intrinsic,
6061 /// depending on the value of isMemMove.
6062 bool TreeToLLVM::EmitBuiltinMemCopy(tree exp, Value *&Result, bool isMemMove,
6063 bool SizeCheck) {
6064 tree arglist = TREE_OPERAND(exp, 1);
6065 if (SizeCheck) {
6066 if (!validate_arglist(arglist, POINTER_TYPE, POINTER_TYPE,
6067 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
6068 return false;
6069 } else {
6070 if (!validate_arglist(arglist, POINTER_TYPE, POINTER_TYPE,
6071 INTEGER_TYPE, VOID_TYPE))
6072 return false;
6075 tree Dst = TREE_VALUE(arglist);
6076 tree Src = TREE_VALUE(TREE_CHAIN(arglist));
6077 unsigned SrcAlign = getPointerAlignment(Src);
6078 unsigned DstAlign = getPointerAlignment(Dst);
6080 Value *DstV = Emit(Dst, 0);
6081 Value *SrcV = Emit(Src, 0);
6082 Value *Len = Emit(TREE_VALUE(TREE_CHAIN(TREE_CHAIN(arglist))), 0);
6083 if (SizeCheck) {
6084 tree SizeArg = TREE_VALUE(TREE_CHAIN(TREE_CHAIN(TREE_CHAIN(arglist))));
6085 Value *Size = Emit(SizeArg, 0);
6086 if (!OptimizeIntoPlainBuiltIn(exp, Len, Size))
6087 return false;
6090 Result = isMemMove ?
6091 EmitMemMove(DstV, SrcV, Len, std::min(SrcAlign, DstAlign)) :
6092 EmitMemCpy(DstV, SrcV, Len, std::min(SrcAlign, DstAlign));
6093 return true;
6096 bool TreeToLLVM::EmitBuiltinMemSet(tree exp, Value *&Result, bool SizeCheck) {
6097 tree arglist = TREE_OPERAND(exp, 1);
6098 if (SizeCheck) {
6099 if (!validate_arglist(arglist, POINTER_TYPE, INTEGER_TYPE,
6100 INTEGER_TYPE, INTEGER_TYPE, VOID_TYPE))
6101 return false;
6102 } else {
6103 if (!validate_arglist(arglist, POINTER_TYPE, INTEGER_TYPE,
6104 INTEGER_TYPE, VOID_TYPE))
6105 return false;
6108 tree Dst = TREE_VALUE(arglist);
6109 unsigned DstAlign = getPointerAlignment(Dst);
6111 Value *DstV = Emit(Dst, 0);
6112 Value *Val = Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0);
6113 Value *Len = Emit(TREE_VALUE(TREE_CHAIN(TREE_CHAIN(arglist))), 0);
6114 if (SizeCheck) {
6115 tree SizeArg = TREE_VALUE(TREE_CHAIN(TREE_CHAIN(TREE_CHAIN(arglist))));
6116 Value *Size = Emit(SizeArg, 0);
6117 if (!OptimizeIntoPlainBuiltIn(exp, Len, Size))
6118 return false;
6120 Result = EmitMemSet(DstV, Val, Len, DstAlign);
6121 return true;
6124 bool TreeToLLVM::EmitBuiltinBZero(tree exp,
6125 Value *&Result ATTRIBUTE_UNUSED) {
6126 tree arglist = TREE_OPERAND(exp, 1);
6127 if (!validate_arglist(arglist, POINTER_TYPE, INTEGER_TYPE, VOID_TYPE))
6128 return false;
6130 tree Dst = TREE_VALUE(arglist);
6131 unsigned DstAlign = getPointerAlignment(Dst);
6133 Value *DstV = Emit(Dst, 0);
6134 Value *Val = Constant::getNullValue(Type::getInt32Ty(Context));
6135 Value *Len = Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0);
6136 EmitMemSet(DstV, Val, Len, DstAlign);
6137 return true;
6140 bool TreeToLLVM::EmitBuiltinPrefetch(tree exp) {
6141 tree arglist = TREE_OPERAND(exp, 1);
6142 if (!validate_arglist(arglist, POINTER_TYPE, 0))
6143 return false;
6145 Value *Ptr = Emit(TREE_VALUE(arglist), 0);
6146 Value *ReadWrite = 0;
6147 Value *Locality = 0;
6149 if (TREE_CHAIN(arglist)) { // Args 1/2 are optional
6150 ReadWrite = Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0);
6151 if (!isa<ConstantInt>(ReadWrite)) {
6152 error("second argument to %<__builtin_prefetch%> must be a constant");
6153 ReadWrite = 0;
6154 } else if (cast<ConstantInt>(ReadWrite)->getZExtValue() > 1) {
6155 warning (0, "invalid second argument to %<__builtin_prefetch%>;"
6156 " using zero");
6157 ReadWrite = 0;
6158 } else {
6159 ReadWrite = Builder.getFolder().CreateIntCast(cast<Constant>(ReadWrite),
6160 Type::getInt32Ty(Context), false);
6163 if (TREE_CHAIN(TREE_CHAIN(arglist))) {
6164 Locality = Emit(TREE_VALUE(TREE_CHAIN(TREE_CHAIN(arglist))), 0);
6165 if (!isa<ConstantInt>(Locality)) {
6166 error("third argument to %<__builtin_prefetch%> must be a constant");
6167 Locality = 0;
6168 } else if (cast<ConstantInt>(Locality)->getZExtValue() > 3) {
6169 warning(0, "invalid third argument to %<__builtin_prefetch%>; using 3");
6170 Locality = 0;
6171 } else {
6172 Locality = Builder.getFolder().CreateIntCast(cast<Constant>(Locality),
6173 Type::getInt32Ty(Context), false);
6178 // Default to highly local read.
6179 if (ReadWrite == 0)
6180 ReadWrite = Constant::getNullValue(Type::getInt32Ty(Context));
6181 if (Locality == 0)
6182 Locality = ConstantInt::get(Type::getInt32Ty(Context), 3);
6184 Ptr = BitCastToType(Ptr, Type::getInt8PtrTy(Context));
6186 Value *Ops[3] = { Ptr, ReadWrite, Locality };
6187 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, Intrinsic::prefetch),
6188 Ops, Ops+3);
6189 return true;
6192 /// EmitBuiltinReturnAddr - Emit an llvm.returnaddress or llvm.frameaddress
6193 /// instruction, depending on whether isFrame is true or not.
6194 bool TreeToLLVM::EmitBuiltinReturnAddr(tree exp, Value *&Result, bool isFrame) {
6195 tree arglist = TREE_OPERAND(exp, 1);
6196 if (!validate_arglist(arglist, INTEGER_TYPE, VOID_TYPE))
6197 return false;
6199 ConstantInt *Level = dyn_cast<ConstantInt>(Emit(TREE_VALUE(arglist), 0));
6200 if (!Level) {
6201 if (isFrame)
6202 error("invalid argument to %<__builtin_frame_address%>");
6203 else
6204 error("invalid argument to %<__builtin_return_address%>");
6205 return false;
6208 Intrinsic::ID IID =
6209 !isFrame ? Intrinsic::returnaddress : Intrinsic::frameaddress;
6210 Result = Builder.CreateCall(Intrinsic::getDeclaration(TheModule, IID), Level);
6211 Result = BitCastToType(Result, ConvertType(TREE_TYPE(exp)));
6212 return true;
6215 bool TreeToLLVM::EmitBuiltinExtractReturnAddr(tree exp, Value *&Result) {
6216 tree arglist = TREE_OPERAND(exp, 1);
6218 Value *Ptr = Emit(TREE_VALUE(arglist), 0);
6220 // FIXME: Actually we should do something like this:
6222 // Result = (Ptr & MASK_RETURN_ADDR) + RETURN_ADDR_OFFSET, if mask and
6223 // offset are defined. This seems to be needed for: ARM, MIPS, Sparc.
6224 // Unfortunately, these constants are defined as RTL expressions and
6225 // should be handled separately.
6227 Result = BitCastToType(Ptr, Type::getInt8PtrTy(Context));
6229 return true;
6232 bool TreeToLLVM::EmitBuiltinFrobReturnAddr(tree exp, Value *&Result) {
6233 tree arglist = TREE_OPERAND(exp, 1);
6235 Value *Ptr = Emit(TREE_VALUE(arglist), 0);
6237 // FIXME: Actually we should do something like this:
6239 // Result = Ptr - RETURN_ADDR_OFFSET, if offset is defined. This seems to be
6240 // needed for: MIPS, Sparc. Unfortunately, these constants are defined
6241 // as RTL expressions and should be handled separately.
6243 Result = BitCastToType(Ptr, Type::getInt8PtrTy(Context));
6245 return true;
6248 bool TreeToLLVM::EmitBuiltinStackSave(tree exp, Value *&Result) {
6249 tree arglist = TREE_OPERAND(exp, 1);
6250 if (!validate_arglist(arglist, VOID_TYPE))
6251 return false;
6253 Result = Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
6254 Intrinsic::stacksave));
6255 return true;
6259 // Builtins used by the exception handling runtime.
6261 // On most machines, the CFA coincides with the first incoming parm.
6262 #ifndef ARG_POINTER_CFA_OFFSET
6263 #define ARG_POINTER_CFA_OFFSET(FNDECL) FIRST_PARM_OFFSET (FNDECL)
6264 #endif
6266 // The mapping from gcc register number to DWARF 2 CFA column number. By
6267 // default, we just provide columns for all registers.
6268 #ifndef DWARF_FRAME_REGNUM
6269 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
6270 #endif
6272 // Map register numbers held in the call frame info that gcc has
6273 // collected using DWARF_FRAME_REGNUM to those that should be output in
6274 // .debug_frame and .eh_frame.
6275 #ifndef DWARF2_FRAME_REG_OUT
6276 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
6277 #endif
6279 /* Registers that get partially clobbered by a call in a given mode.
6280 These must not be call used registers. */
6281 #ifndef HARD_REGNO_CALL_PART_CLOBBERED
6282 #define HARD_REGNO_CALL_PART_CLOBBERED(REGNO, MODE) 0
6283 #endif
6285 bool TreeToLLVM::EmitBuiltinDwarfCFA(tree exp, Value *&Result) {
6286 if (!validate_arglist(TREE_OPERAND(exp, 1), VOID_TYPE))
6287 return false;
6289 int cfa_offset = ARG_POINTER_CFA_OFFSET(exp);
6291 // FIXME: is i32 always enough here?
6292 Result = Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
6293 Intrinsic::eh_dwarf_cfa),
6294 ConstantInt::get(Type::getInt32Ty(Context), cfa_offset));
6296 return true;
6299 bool TreeToLLVM::EmitBuiltinDwarfSPColumn(tree exp, Value *&Result) {
6300 if (!validate_arglist(TREE_OPERAND(exp, 1), VOID_TYPE))
6301 return false;
6303 unsigned int dwarf_regnum = DWARF_FRAME_REGNUM(STACK_POINTER_REGNUM);
6304 Result = ConstantInt::get(ConvertType(TREE_TYPE(exp)), dwarf_regnum);
6306 return true;
6309 bool TreeToLLVM::EmitBuiltinEHReturnDataRegno(tree exp, Value *&Result) {
6310 #ifdef EH_RETURN_DATA_REGNO
6311 tree arglist = TREE_OPERAND(exp, 1);
6313 if (!validate_arglist(arglist, INTEGER_TYPE, VOID_TYPE))
6314 return false;
6316 tree which = TREE_VALUE (arglist);
6317 unsigned HOST_WIDE_INT iwhich;
6319 if (TREE_CODE (which) != INTEGER_CST) {
6320 error ("argument of %<__builtin_eh_return_regno%> must be constant");
6321 return false;
6324 iwhich = tree_low_cst (which, 1);
6325 iwhich = EH_RETURN_DATA_REGNO (iwhich);
6326 if (iwhich == INVALID_REGNUM)
6327 return false;
6329 iwhich = DWARF_FRAME_REGNUM (iwhich);
6331 Result = ConstantInt::get(ConvertType(TREE_TYPE(exp)), iwhich);
6332 #endif
6334 return true;
6337 bool TreeToLLVM::EmitBuiltinEHReturn(tree exp, Value *&Result) {
6338 tree arglist = TREE_OPERAND(exp, 1);
6340 if (!validate_arglist(arglist, INTEGER_TYPE, POINTER_TYPE, VOID_TYPE))
6341 return false;
6343 const Type *IntPtr = TD.getIntPtrType(Context);
6344 Value *Offset = Emit(TREE_VALUE(arglist), 0);
6345 Value *Handler = Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0);
6347 Intrinsic::ID IID = IntPtr->isInteger(32) ?
6348 Intrinsic::eh_return_i32 : Intrinsic::eh_return_i64;
6350 Offset = Builder.CreateIntCast(Offset, IntPtr, true);
6351 Handler = BitCastToType(Handler, Type::getInt8PtrTy(Context));
6353 SmallVector<Value *, 2> Args;
6354 Args.push_back(Offset);
6355 Args.push_back(Handler);
6356 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, IID),
6357 Args.begin(), Args.end());
6358 Result = Builder.CreateUnreachable();
6359 EmitBlock(BasicBlock::Create(Context, ""));
6361 return true;
6364 bool TreeToLLVM::EmitBuiltinInitDwarfRegSizes(tree exp,
6365 Value *&Result ATTRIBUTE_UNUSED) {
6366 #ifdef DWARF2_UNWIND_INFO
6367 unsigned int i;
6368 bool wrote_return_column = false;
6369 static bool reg_modes_initialized = false;
6371 tree arglist = TREE_OPERAND(exp, 1);
6372 if (!validate_arglist(arglist, POINTER_TYPE, VOID_TYPE))
6373 return false;
6375 if (!reg_modes_initialized) {
6376 init_reg_modes_once();
6377 reg_modes_initialized = true;
6380 Value *Addr = BitCastToType(Emit(TREE_VALUE(arglist), 0),
6381 Type::getInt8PtrTy(Context));
6382 Constant *Size, *Idx;
6384 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) {
6385 int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
6387 if (rnum < DWARF_FRAME_REGISTERS) {
6388 enum machine_mode save_mode = reg_raw_mode[i];
6389 HOST_WIDE_INT size;
6391 if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
6392 save_mode = choose_hard_reg_mode (i, 1, true);
6393 if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN) {
6394 if (save_mode == VOIDmode)
6395 continue;
6396 wrote_return_column = true;
6398 size = GET_MODE_SIZE (save_mode);
6399 if (rnum < 0)
6400 continue;
6402 Size = ConstantInt::get(Type::getInt8Ty(Context), size);
6403 Idx = ConstantInt::get(Type::getInt32Ty(Context), rnum);
6404 Builder.CreateStore(Size, Builder.CreateGEP(Addr, Idx), false);
6408 if (!wrote_return_column) {
6409 Size = ConstantInt::get(Type::getInt8Ty(Context), GET_MODE_SIZE (Pmode));
6410 Idx = ConstantInt::get(Type::getInt32Ty(Context), DWARF_FRAME_RETURN_COLUMN);
6411 Builder.CreateStore(Size, Builder.CreateGEP(Addr, Idx), false);
6414 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
6415 Size = ConstantInt::get(Type::getInt8Ty(Context), GET_MODE_SIZE (Pmode));
6416 Idx = ConstantInt::get(Type::getInt32Ty(Context), DWARF_ALT_FRAME_RETURN_COLUMN);
6417 Builder.CreateStore(Size, Builder.CreateGEP(Addr, Idx), false);
6418 #endif
6420 #endif /* DWARF2_UNWIND_INFO */
6422 // TODO: the RS6000 target needs extra initialization [gcc changeset 122468].
6424 return true;
6427 bool TreeToLLVM::EmitBuiltinUnwindInit(tree exp, Value *&Result) {
6428 if (!validate_arglist(TREE_OPERAND(exp, 1), VOID_TYPE))
6429 return false;
6431 Result = Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
6432 Intrinsic::eh_unwind_init));
6434 return true;
6437 bool TreeToLLVM::EmitBuiltinStackRestore(tree exp) {
6438 tree arglist = TREE_OPERAND(exp, 1);
6439 if (!validate_arglist(arglist, POINTER_TYPE, VOID_TYPE))
6440 return false;
6442 Value *Ptr = Emit(TREE_VALUE(arglist), 0);
6443 Ptr = BitCastToType(Ptr, Type::getInt8PtrTy(Context));
6445 Builder.CreateCall(Intrinsic::getDeclaration(TheModule,
6446 Intrinsic::stackrestore), Ptr);
6447 return true;
6451 bool TreeToLLVM::EmitBuiltinAlloca(tree exp, Value *&Result) {
6452 tree arglist = TREE_OPERAND(exp, 1);
6453 if (!validate_arglist(arglist, INTEGER_TYPE, VOID_TYPE))
6454 return false;
6455 Value *Amt = Emit(TREE_VALUE(arglist), 0);
6456 Amt = CastToSIntType(Amt, Type::getInt32Ty(Context));
6457 Result = Builder.CreateAlloca(Type::getInt8Ty(Context), Amt);
6458 return true;
6461 bool TreeToLLVM::EmitBuiltinExpect(tree exp, const MemRef *DestLoc,
6462 Value *&Result) {
6463 // Ignore the hint for now, just expand the expr. This is safe, but not
6464 // optimal.
6465 tree arglist = TREE_OPERAND(exp, 1);
6466 if (arglist == NULL_TREE || TREE_CHAIN(arglist) == NULL_TREE)
6467 return true;
6468 Result = Emit(TREE_VALUE(arglist), DestLoc);
6469 return true;
6472 bool TreeToLLVM::EmitBuiltinVAStart(tree exp) {
6473 tree arglist = TREE_OPERAND(exp, 1);
6474 tree fntype = TREE_TYPE(current_function_decl);
6476 if (TYPE_ARG_TYPES(fntype) == 0 ||
6477 (TREE_VALUE(tree_last(TYPE_ARG_TYPES(fntype))) == void_type_node)) {
6478 error("`va_start' used in function with fixed args");
6479 return true;
6482 tree chain = TREE_CHAIN(arglist);
6484 // Check for errors.
6485 if (fold_builtin_next_arg (chain))
6486 return true;
6488 Value *ArgVal = Emit(TREE_VALUE(arglist), 0);
6490 Constant *llvm_va_start_fn = Intrinsic::getDeclaration(TheModule,
6491 Intrinsic::vastart);
6492 ArgVal = BitCastToType(ArgVal, Type::getInt8PtrTy(Context));
6493 Builder.CreateCall(llvm_va_start_fn, ArgVal);
6494 return true;
6497 bool TreeToLLVM::EmitBuiltinVAEnd(tree exp) {
6498 Value *Arg = Emit(TREE_VALUE(TREE_OPERAND(exp, 1)), 0);
6499 Arg = BitCastToType(Arg, Type::getInt8PtrTy(Context));
6500 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, Intrinsic::vaend),
6501 Arg);
6502 return true;
6505 bool TreeToLLVM::EmitBuiltinVACopy(tree exp) {
6506 tree Arg1T = TREE_VALUE(TREE_OPERAND(exp, 1));
6507 tree Arg2T = TREE_VALUE(TREE_CHAIN(TREE_OPERAND(exp, 1)));
6509 Value *Arg1 = Emit(Arg1T, 0); // Emit the address of the destination.
6510 // The second arg of llvm.va_copy is a pointer to a valist.
6511 Value *Arg2;
6512 if (!isAggregateTreeType(va_list_type_node)) {
6513 // Emit it as a value, then store it to a temporary slot.
6514 Value *V2 = Emit(Arg2T, 0);
6515 Arg2 = CreateTemporary(V2->getType());
6516 Builder.CreateStore(V2, Arg2);
6517 } else {
6518 // If the target has aggregate valists, then the second argument
6519 // from GCC is the address of the source valist and we don't
6520 // need to do anything special.
6521 Arg2 = Emit(Arg2T, 0);
6524 static const Type *VPTy = Type::getInt8PtrTy(Context);
6526 // FIXME: This ignores alignment and volatility of the arguments.
6527 SmallVector<Value *, 2> Args;
6528 Args.push_back(BitCastToType(Arg1, VPTy));
6529 Args.push_back(BitCastToType(Arg2, VPTy));
6531 Builder.CreateCall(Intrinsic::getDeclaration(TheModule, Intrinsic::vacopy),
6532 Args.begin(), Args.end());
6533 return true;
6536 bool TreeToLLVM::EmitBuiltinInitTrampoline(tree exp, Value *&Result) {
6537 tree arglist = TREE_OPERAND(exp, 1);
6538 if (!validate_arglist (arglist, POINTER_TYPE, POINTER_TYPE, POINTER_TYPE,
6539 VOID_TYPE))
6540 return false;
6542 static const Type *VPTy = Type::getInt8PtrTy(Context);
6544 Value *Tramp = Emit(TREE_VALUE(arglist), 0);
6545 Tramp = BitCastToType(Tramp, VPTy);
6547 Value *Func = Emit(TREE_VALUE(TREE_CHAIN(arglist)), 0);
6548 Func = BitCastToType(Func, VPTy);
6550 Value *Chain = Emit(TREE_VALUE(TREE_CHAIN(TREE_CHAIN(arglist))), 0);
6551 Chain = BitCastToType(Chain, VPTy);
6553 Value *Ops[3] = { Tramp, Func, Chain };
6555 Function *Intr = Intrinsic::getDeclaration(TheModule,
6556 Intrinsic::init_trampoline);
6557 Result = Builder.CreateCall(Intr, Ops, Ops+3, "tramp");
6558 return true;
6561 //===----------------------------------------------------------------------===//
6562 // ... Complex Math Expressions ...
6563 //===----------------------------------------------------------------------===//
6565 void TreeToLLVM::EmitLoadFromComplex(Value *&Real, Value *&Imag,
6566 MemRef SrcComplex) {
6567 Value *RealPtr = Builder.CreateStructGEP(SrcComplex.Ptr, 0, "real");
6568 Real = Builder.CreateLoad(RealPtr, SrcComplex.Volatile, "real");
6569 cast<LoadInst>(Real)->setAlignment(SrcComplex.getAlignment());
6571 Value *ImagPtr = Builder.CreateStructGEP(SrcComplex.Ptr, 1, "imag");
6572 Imag = Builder.CreateLoad(ImagPtr, SrcComplex.Volatile, "imag");
6573 cast<LoadInst>(Imag)->setAlignment(
6574 MinAlign(SrcComplex.getAlignment(), TD.getTypeAllocSize(Real->getType()))
6578 void TreeToLLVM::EmitStoreToComplex(MemRef DestComplex, Value *Real,
6579 Value *Imag) {
6580 StoreInst *St;
6582 Value *RealPtr = Builder.CreateStructGEP(DestComplex.Ptr, 0, "real");
6583 St = Builder.CreateStore(Real, RealPtr, DestComplex.Volatile);
6584 St->setAlignment(DestComplex.getAlignment());
6586 Value *ImagPtr = Builder.CreateStructGEP(DestComplex.Ptr, 1, "imag");
6587 St = Builder.CreateStore(Imag, ImagPtr, DestComplex.Volatile);
6588 St->setAlignment(
6589 MinAlign(DestComplex.getAlignment(), TD.getTypeAllocSize(Real->getType()))
6594 void TreeToLLVM::EmitCOMPLEX_EXPR(tree exp, const MemRef *DestLoc) {
6595 Value *Real = Emit(TREE_OPERAND(exp, 0), 0);
6596 Value *Imag = Emit(TREE_OPERAND(exp, 1), 0);
6597 EmitStoreToComplex(*DestLoc, Real, Imag);
6600 void TreeToLLVM::EmitCOMPLEX_CST(tree exp, const MemRef *DestLoc) {
6601 Value *Real = Emit(TREE_REALPART(exp), 0);
6602 Value *Imag = Emit(TREE_IMAGPART(exp), 0);
6603 EmitStoreToComplex(*DestLoc, Real, Imag);
6606 // EmitComplexBinOp - Note that this operates on binops like ==/!=, which return
6607 // a bool, not a complex value.
6608 Value *TreeToLLVM::EmitComplexBinOp(tree exp, const MemRef *DestLoc) {
6609 const Type *ComplexTy = ConvertType(TREE_TYPE(TREE_OPERAND(exp, 0)));
6611 MemRef LHSTmp = CreateTempLoc(ComplexTy);
6612 MemRef RHSTmp = CreateTempLoc(ComplexTy);
6613 Emit(TREE_OPERAND(exp, 0), &LHSTmp);
6614 Emit(TREE_OPERAND(exp, 1), &RHSTmp);
6616 Value *LHSr, *LHSi;
6617 EmitLoadFromComplex(LHSr, LHSi, LHSTmp);
6618 Value *RHSr, *RHSi;
6619 EmitLoadFromComplex(RHSr, RHSi, RHSTmp);
6621 Value *DSTr, *DSTi;
6622 switch (TREE_CODE(exp)) {
6623 default: TODO(exp);
6624 case PLUS_EXPR: // (a+ib) + (c+id) = (a+c) + i(b+d)
6625 if (LHSr->getType()->isFloatingPoint()) {
6626 DSTr = Builder.CreateFAdd(LHSr, RHSr, "tmpr");
6627 DSTi = Builder.CreateFAdd(LHSi, RHSi, "tmpi");
6628 } else {
6629 DSTr = Builder.CreateAdd(LHSr, RHSr, "tmpr");
6630 DSTi = Builder.CreateAdd(LHSi, RHSi, "tmpi");
6632 break;
6633 case MINUS_EXPR: // (a+ib) - (c+id) = (a-c) + i(b-d)
6634 if (LHSr->getType()->isFloatingPoint()) {
6635 DSTr = Builder.CreateFSub(LHSr, RHSr, "tmpr");
6636 DSTi = Builder.CreateFSub(LHSi, RHSi, "tmpi");
6637 } else {
6638 DSTr = Builder.CreateSub(LHSr, RHSr, "tmpr");
6639 DSTi = Builder.CreateSub(LHSi, RHSi, "tmpi");
6641 break;
6642 case MULT_EXPR: { // (a+ib) * (c+id) = (ac-bd) + i(ad+cb)
6643 if (LHSr->getType()->isFloatingPoint()) {
6644 Value *Tmp1 = Builder.CreateFMul(LHSr, RHSr); // a*c
6645 Value *Tmp2 = Builder.CreateFMul(LHSi, RHSi); // b*d
6646 DSTr = Builder.CreateFSub(Tmp1, Tmp2); // ac-bd
6648 Value *Tmp3 = Builder.CreateFMul(LHSr, RHSi); // a*d
6649 Value *Tmp4 = Builder.CreateFMul(RHSr, LHSi); // c*b
6650 DSTi = Builder.CreateFAdd(Tmp3, Tmp4); // ad+cb
6651 } else {
6652 Value *Tmp1 = Builder.CreateMul(LHSr, RHSr); // a*c
6653 Value *Tmp2 = Builder.CreateMul(LHSi, RHSi); // b*d
6654 DSTr = Builder.CreateSub(Tmp1, Tmp2); // ac-bd
6656 Value *Tmp3 = Builder.CreateMul(LHSr, RHSi); // a*d
6657 Value *Tmp4 = Builder.CreateMul(RHSr, LHSi); // c*b
6658 DSTi = Builder.CreateAdd(Tmp3, Tmp4); // ad+cb
6660 break;
6662 case RDIV_EXPR: { // (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
6663 // RDIV_EXPR should always be floating point.
6664 assert (LHSr->getType()->isFloatingPoint());
6665 Value *Tmp1 = Builder.CreateFMul(LHSr, RHSr); // a*c
6666 Value *Tmp2 = Builder.CreateFMul(LHSi, RHSi); // b*d
6667 Value *Tmp3 = Builder.CreateFAdd(Tmp1, Tmp2); // ac+bd
6669 Value *Tmp4 = Builder.CreateFMul(RHSr, RHSr); // c*c
6670 Value *Tmp5 = Builder.CreateFMul(RHSi, RHSi); // d*d
6671 Value *Tmp6 = Builder.CreateFAdd(Tmp4, Tmp5); // cc+dd
6672 DSTr = Builder.CreateFDiv(Tmp3, Tmp6);
6674 Value *Tmp7 = Builder.CreateFMul(LHSi, RHSr); // b*c
6675 Value *Tmp8 = Builder.CreateFMul(LHSr, RHSi); // a*d
6676 Value *Tmp9 = Builder.CreateFSub(Tmp7, Tmp8); // bc-ad
6677 DSTi = Builder.CreateFDiv(Tmp9, Tmp6);
6678 break;
6680 case EQ_EXPR: // (a+ib) == (c+id) = (a == c) & (b == d)
6681 if (LHSr->getType()->isFloatingPoint()) {
6682 DSTr = Builder.CreateFCmpOEQ(LHSr, RHSr, "tmpr");
6683 DSTi = Builder.CreateFCmpOEQ(LHSi, RHSi, "tmpi");
6684 } else {
6685 DSTr = Builder.CreateICmpEQ(LHSr, RHSr, "tmpr");
6686 DSTi = Builder.CreateICmpEQ(LHSi, RHSi, "tmpi");
6688 return Builder.CreateAnd(DSTr, DSTi);
6689 case NE_EXPR: // (a+ib) != (c+id) = (a != c) | (b != d)
6690 if (LHSr->getType()->isFloatingPoint()) {
6691 DSTr = Builder.CreateFCmpUNE(LHSr, RHSr, "tmpr");
6692 DSTi = Builder.CreateFCmpUNE(LHSi, RHSi, "tmpi");
6693 } else {
6694 DSTr = Builder.CreateICmpNE(LHSr, RHSr, "tmpr");
6695 DSTi = Builder.CreateICmpNE(LHSi, RHSi, "tmpi");
6697 return Builder.CreateOr(DSTr, DSTi);
6700 EmitStoreToComplex(*DestLoc, DSTr, DSTi);
6701 return 0;
6705 //===----------------------------------------------------------------------===//
6706 // ... L-Value Expressions ...
6707 //===----------------------------------------------------------------------===//
6709 /// getFieldOffsetInBits - Return the offset (in bits) of a FIELD_DECL in a
6710 /// structure.
6711 static unsigned getFieldOffsetInBits(tree Field) {
6712 assert(DECL_FIELD_BIT_OFFSET(Field) != 0 && DECL_FIELD_OFFSET(Field) != 0);
6713 unsigned Result = TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(Field));
6714 if (TREE_CODE(DECL_FIELD_OFFSET(Field)) == INTEGER_CST)
6715 Result += TREE_INT_CST_LOW(DECL_FIELD_OFFSET(Field))*8;
6716 return Result;
6719 /// getComponentRefOffsetInBits - Return the offset (in bits) of the field
6720 /// referenced in a COMPONENT_REF exp.
6721 static unsigned getComponentRefOffsetInBits(tree exp) {
6722 assert(TREE_CODE(exp) == COMPONENT_REF && "not a COMPONENT_REF!");
6723 tree field = TREE_OPERAND(exp, 1);
6724 assert(TREE_CODE(field) == FIELD_DECL && "not a FIELD_DECL!");
6725 tree field_offset = component_ref_field_offset (exp);
6726 assert(DECL_FIELD_BIT_OFFSET(field) && field_offset);
6727 unsigned Result = TREE_INT_CST_LOW(DECL_FIELD_BIT_OFFSET(field));
6728 if (TREE_CODE(field_offset) == INTEGER_CST)
6729 Result += TREE_INT_CST_LOW(field_offset)*8;
6730 return Result;
6733 Value *TreeToLLVM::EmitFieldAnnotation(Value *FieldPtr, tree FieldDecl) {
6734 tree AnnotateAttr = lookup_attribute("annotate", DECL_ATTRIBUTES(FieldDecl));
6736 const Type *SBP = Type::getInt8PtrTy(Context);
6738 Function *Fn = Intrinsic::getDeclaration(TheModule,
6739 Intrinsic::ptr_annotation,
6740 &SBP, 1);
6742 // Get file and line number. FIXME: Should this be for the decl or the
6743 // use. Is there a location info for the use?
6744 Constant *LineNo = ConstantInt::get(Type::getInt32Ty(Context),
6745 DECL_SOURCE_LINE(FieldDecl));
6746 Constant *File = ConvertMetadataStringToGV(DECL_SOURCE_FILE(FieldDecl));
6748 File = TheFolder->CreateBitCast(File, SBP);
6750 // There may be multiple annotate attributes. Pass return of lookup_attr
6751 // to successive lookups.
6752 while (AnnotateAttr) {
6753 // Each annotate attribute is a tree list.
6754 // Get value of list which is our linked list of args.
6755 tree args = TREE_VALUE(AnnotateAttr);
6757 // Each annotate attribute may have multiple args.
6758 // Treat each arg as if it were a separate annotate attribute.
6759 for (tree a = args; a; a = TREE_CHAIN(a)) {
6760 // Each element of the arg list is a tree list, so get value
6761 tree val = TREE_VALUE(a);
6763 // Assert its a string, and then get that string.
6764 assert(TREE_CODE(val) == STRING_CST &&
6765 "Annotate attribute arg should always be a string");
6767 Constant *strGV = TreeConstantToLLVM::EmitLV_STRING_CST(val);
6769 // We can not use the IRBuilder because it will constant fold away
6770 // the GEP that is critical to distinguish between an annotate
6771 // attribute on a whole struct from one on the first element of the
6772 // struct.
6773 BitCastInst *CastFieldPtr = new BitCastInst(FieldPtr, SBP,
6774 FieldPtr->getName());
6775 Builder.Insert(CastFieldPtr);
6777 Value *Ops[4] = {
6778 CastFieldPtr, BitCastToType(strGV, SBP),
6779 File, LineNo
6782 const Type* FieldPtrType = FieldPtr->getType();
6783 FieldPtr = Builder.CreateCall(Fn, Ops, Ops+4);
6784 FieldPtr = BitCastToType(FieldPtr, FieldPtrType);
6787 // Get next annotate attribute.
6788 AnnotateAttr = TREE_CHAIN(AnnotateAttr);
6789 if (AnnotateAttr)
6790 AnnotateAttr = lookup_attribute("annotate", AnnotateAttr);
6792 return FieldPtr;
6795 LValue TreeToLLVM::EmitLV_ARRAY_REF(tree exp) {
6796 // The result type is an ElementTy* in the case of an ARRAY_REF, an array
6797 // of ElementTy in the case of ARRAY_RANGE_REF.
6799 tree Array = TREE_OPERAND(exp, 0);
6800 tree ArrayTreeType = TREE_TYPE(Array);
6801 tree Index = TREE_OPERAND(exp, 1);
6802 tree IndexType = TREE_TYPE(Index);
6803 tree ElementType = TREE_TYPE(ArrayTreeType);
6805 assert((TREE_CODE (ArrayTreeType) == ARRAY_TYPE ||
6806 TREE_CODE (ArrayTreeType) == POINTER_TYPE ||
6807 TREE_CODE (ArrayTreeType) == REFERENCE_TYPE ||
6808 TREE_CODE (ArrayTreeType) == BLOCK_POINTER_TYPE) &&
6809 "Unknown ARRAY_REF!");
6811 // As an LLVM extension, we allow ARRAY_REF with a pointer as the first
6812 // operand. This construct maps directly to a getelementptr instruction.
6813 Value *ArrayAddr;
6814 unsigned ArrayAlign;
6816 if (TREE_CODE(ArrayTreeType) == ARRAY_TYPE) {
6817 // First subtract the lower bound, if any, in the type of the index.
6818 tree LowerBound = array_ref_low_bound(exp);
6819 if (!integer_zerop(LowerBound))
6820 Index = fold(build2(MINUS_EXPR, IndexType, Index, LowerBound));
6822 LValue ArrayAddrLV = EmitLV(Array);
6823 assert(!ArrayAddrLV.isBitfield() && "Arrays cannot be bitfields!");
6824 ArrayAddr = ArrayAddrLV.Ptr;
6825 ArrayAlign = ArrayAddrLV.getAlignment();
6826 } else {
6827 ArrayAddr = Emit(Array, 0);
6828 if (TREE_CODE (ArrayTreeType) == POINTER_TYPE)
6829 ArrayAlign = getPointerAlignment(Array);
6830 else
6831 ArrayAlign = 1;
6834 Value *IndexVal = Emit(Index, 0);
6836 const Type *IntPtrTy = getTargetData().getIntPtrType(Context);
6837 if (TYPE_UNSIGNED(IndexType)) // if the index is unsigned
6838 // ZExt it to retain its value in the larger type
6839 IndexVal = CastToUIntType(IndexVal, IntPtrTy);
6840 else
6841 // SExt it to retain its value in the larger type
6842 IndexVal = CastToSIntType(IndexVal, IntPtrTy);
6844 // If we are indexing over a fixed-size type, just use a GEP.
6845 if (isSequentialCompatible(ArrayTreeType)) {
6846 SmallVector<Value*, 2> Idx;
6847 if (TREE_CODE(ArrayTreeType) == ARRAY_TYPE)
6848 Idx.push_back(ConstantInt::get(IntPtrTy, 0));
6849 Idx.push_back(IndexVal);
6850 Value *Ptr = flag_wrapv ?
6851 Builder.CreateGEP(ArrayAddr, Idx.begin(), Idx.end()) :
6852 Builder.CreateInBoundsGEP(ArrayAddr, Idx.begin(), Idx.end());
6854 const Type *ElementTy = ConvertType(ElementType);
6855 unsigned Alignment = MinAlign(ArrayAlign, TD.getABITypeAlignment(ElementTy));
6856 return LValue(BitCastToType(Ptr,
6857 ConvertType(TREE_TYPE(exp))->getPointerTo()),
6858 Alignment);
6861 // Otherwise, just do raw, low-level pointer arithmetic. FIXME: this could be
6862 // much nicer in cases like:
6863 // float foo(int w, float A[][w], int g) { return A[g][0]; }
6865 ArrayAddr = BitCastToType(ArrayAddr,
6866 Type::getInt8PtrTy(Context));
6867 if (VOID_TYPE_P(TREE_TYPE(ArrayTreeType)))
6868 return LValue(Builder.CreateGEP(ArrayAddr, IndexVal), 1);
6870 Value *TypeSize = Emit(array_ref_element_size(exp), 0);
6871 TypeSize = CastToUIntType(TypeSize, IntPtrTy);
6872 IndexVal = Builder.CreateMul(IndexVal, TypeSize);
6873 unsigned Alignment = 1;
6874 if (isa<ConstantInt>(IndexVal))
6875 Alignment = MinAlign(ArrayAlign,
6876 cast<ConstantInt>(IndexVal)->getZExtValue());
6877 Value *Ptr = flag_wrapv ?
6878 Builder.CreateGEP(ArrayAddr, IndexVal) :
6879 Builder.CreateInBoundsGEP(ArrayAddr, IndexVal);
6880 return LValue(BitCastToType(Ptr, ConvertType(TREE_TYPE(exp))->getPointerTo()),
6881 Alignment);
6884 LValue TreeToLLVM::EmitLV_BIT_FIELD_REF(tree exp) {
6885 LValue Ptr = EmitLV(TREE_OPERAND(exp, 0));
6886 assert(!Ptr.isBitfield() && "BIT_FIELD_REF operands cannot be bitfields!");
6888 unsigned BitStart = (unsigned)TREE_INT_CST_LOW(TREE_OPERAND(exp, 2));
6889 unsigned BitSize = (unsigned)TREE_INT_CST_LOW(TREE_OPERAND(exp, 1));
6890 const Type *ValTy = ConvertType(TREE_TYPE(exp));
6892 unsigned ValueSizeInBits = TD.getTypeSizeInBits(ValTy);
6893 assert(BitSize <= ValueSizeInBits &&
6894 "ValTy isn't large enough to hold the value loaded!");
6896 assert(ValueSizeInBits == TD.getTypeAllocSizeInBits(ValTy) &&
6897 "FIXME: BIT_FIELD_REF logic is broken for non-round types");
6899 // BIT_FIELD_REF values can have BitStart values that are quite large. We
6900 // know that the thing we are loading is ValueSizeInBits large. If BitStart
6901 // is larger than ValueSizeInBits, bump the pointer over to where it should
6902 // be.
6903 if (unsigned UnitOffset = BitStart / ValueSizeInBits) {
6904 // TODO: If Ptr.Ptr is a struct type or something, we can do much better
6905 // than this. e.g. check out when compiling unwind-dw2-fde-darwin.c.
6906 Ptr.Ptr = BitCastToType(Ptr.Ptr, ValTy->getPointerTo());
6907 Ptr.Ptr = Builder.CreateGEP(Ptr.Ptr,
6908 ConstantInt::get(Type::getInt32Ty(Context), UnitOffset));
6909 BitStart -= UnitOffset*ValueSizeInBits;
6912 // If this is referring to the whole field, return the whole thing.
6913 if (BitStart == 0 && BitSize == ValueSizeInBits) {
6914 return LValue(BitCastToType(Ptr.Ptr, ValTy->getPointerTo()),
6915 Ptr.getAlignment());
6918 return LValue(BitCastToType(Ptr.Ptr, ValTy->getPointerTo()), 1,
6919 BitStart, BitSize);
6922 LValue TreeToLLVM::EmitLV_COMPONENT_REF(tree exp) {
6923 LValue StructAddrLV = EmitLV(TREE_OPERAND(exp, 0));
6924 tree FieldDecl = TREE_OPERAND(exp, 1);
6925 unsigned LVAlign = StructAddrLV.getAlignment();
6927 assert((TREE_CODE(DECL_CONTEXT(FieldDecl)) == RECORD_TYPE ||
6928 TREE_CODE(DECL_CONTEXT(FieldDecl)) == UNION_TYPE ||
6929 TREE_CODE(DECL_CONTEXT(FieldDecl)) == QUAL_UNION_TYPE));
6931 // Ensure that the struct type has been converted, so that the fielddecls
6932 // are laid out. Note that we convert to the context of the Field, not to the
6933 // type of Operand #0, because GCC doesn't always have the field match up with
6934 // operand #0's type.
6935 const Type *StructTy = ConvertType(DECL_CONTEXT(FieldDecl));
6937 assert((!StructAddrLV.isBitfield() ||
6938 StructAddrLV.BitStart == 0) && "structs cannot be bitfields!");
6940 StructAddrLV.Ptr = BitCastToType(StructAddrLV.Ptr,
6941 StructTy->getPointerTo());
6942 const Type *FieldTy = ConvertType(getDeclaredType(FieldDecl));
6944 // BitStart - This is the actual offset of the field from the start of the
6945 // struct, in bits. For bitfields this may be on a non-byte boundary.
6946 unsigned BitStart = getComponentRefOffsetInBits(exp);
6947 Value *FieldPtr;
6949 tree field_offset = component_ref_field_offset (exp);
6950 // If this is a normal field at a fixed offset from the start, handle it.
6951 if (TREE_CODE(field_offset) == INTEGER_CST) {
6952 unsigned int MemberIndex = GET_LLVM_FIELD_INDEX(FieldDecl);
6954 // If the LLVM struct has zero field, don't try to index into it, just use
6955 // the current pointer.
6956 FieldPtr = StructAddrLV.Ptr;
6957 if (StructTy->getNumContainedTypes() != 0) {
6958 assert(MemberIndex < StructTy->getNumContainedTypes() &&
6959 "Field Idx out of range!");
6960 FieldPtr = Builder.CreateStructGEP(FieldPtr, MemberIndex);
6963 // Now that we did an offset from the start of the struct, subtract off
6964 // the offset from BitStart.
6965 if (MemberIndex) {
6966 const StructLayout *SL = TD.getStructLayout(cast<StructType>(StructTy));
6967 unsigned Offset = SL->getElementOffset(MemberIndex);
6968 BitStart -= Offset * 8;
6970 // If the base is known to be 8-byte aligned, and we're adding a 4-byte
6971 // offset, the field is known to be 4-byte aligned.
6972 LVAlign = MinAlign(LVAlign, Offset);
6975 // There is debate about whether this is really safe or not, be conservative
6976 // in the meantime.
6977 #if 0
6978 // If this field is at a constant offset, if the LLVM pointer really points
6979 // to it, then we know that the pointer is at least as aligned as the field
6980 // is required to be. Try to round up our alignment info.
6981 if (BitStart == 0 && // llvm pointer points to it.
6982 !isBitfield(FieldDecl) && // bitfield computation might offset pointer.
6983 DECL_ALIGN(FieldDecl))
6984 LVAlign = std::max(LVAlign, unsigned(DECL_ALIGN(FieldDecl)) / 8);
6985 #endif
6987 // If the FIELD_DECL has an annotate attribute on it, emit it.
6988 if (lookup_attribute("annotate", DECL_ATTRIBUTES(FieldDecl)))
6989 FieldPtr = EmitFieldAnnotation(FieldPtr, FieldDecl);
6990 } else {
6991 Value *Offset = Emit(field_offset, 0);
6993 // For ObjC2, the offset of the field is loaded from memory (it can
6994 // change at runtime), and the initial value in memory includes the
6995 // value that would normally be computed at compile time; we don't
6996 // want to add this in twice. The ObjC FE figures out the value we
6997 // actually should add at compile time (usually 0).
6998 tree field_bit_offset = objc_v2_bitfield_ivar_bitpos(exp);
6999 if (field_bit_offset) {
7000 BitStart = (unsigned)getINTEGER_CSTVal(field_bit_offset);
7002 // Here BitStart gives the offset of the field in bits from field_offset.
7003 // Incorporate as much of it as possible into the pointer computation.
7004 unsigned ByteOffset = BitStart/8;
7005 if (ByteOffset > 0) {
7006 Offset = Builder.CreateAdd(Offset,
7007 ConstantInt::get(Offset->getType(), ByteOffset));
7008 BitStart -= ByteOffset*8;
7009 // If the base is known to be 8-byte aligned, and we're adding a 4-byte
7010 // offset, the field is known to be 4-byte aligned.
7011 LVAlign = MinAlign(LVAlign, ByteOffset);
7014 Value *Ptr = CastToType(Instruction::PtrToInt, StructAddrLV.Ptr,
7015 Offset->getType());
7016 Ptr = Builder.CreateAdd(Ptr, Offset);
7017 FieldPtr = CastToType(Instruction::IntToPtr, Ptr,
7018 FieldTy->getPointerTo());
7021 if (isBitfield(FieldDecl)) {
7022 // If this is a bitfield, the declared type must be an integral type.
7023 assert(FieldTy->isInteger() && "Invalid bitfield");
7025 assert(DECL_SIZE(FieldDecl) &&
7026 TREE_CODE(DECL_SIZE(FieldDecl)) == INTEGER_CST &&
7027 "Variable sized bitfield?");
7028 unsigned BitfieldSize = TREE_INT_CST_LOW(DECL_SIZE(FieldDecl));
7030 const Type *LLVMFieldTy =
7031 cast<PointerType>(FieldPtr->getType())->getElementType();
7033 // If the LLVM notion of the field type contains the entire bitfield being
7034 // accessed, use the LLVM type. This avoids pointer casts and other bad
7035 // things that are difficult to clean up later. This occurs in cases like
7036 // "struct X{ unsigned long long x:50; unsigned y:2; }" when accessing y.
7037 // We want to access the field as a ulong, not as a uint with an offset.
7038 if (LLVMFieldTy->isInteger() &&
7039 LLVMFieldTy->getPrimitiveSizeInBits() >= BitStart + BitfieldSize &&
7040 LLVMFieldTy->getPrimitiveSizeInBits() ==
7041 TD.getTypeAllocSizeInBits(LLVMFieldTy))
7042 FieldTy = LLVMFieldTy;
7043 else
7044 // If the field result type T is a bool or some other curiously sized
7045 // integer type, then not all bits may be accessible by advancing a T*
7046 // and loading through it. For example, if the result type is i1 then
7047 // only the first bit in each byte would be loaded. Even if T is byte
7048 // sized like an i24 there may be trouble: incrementing a T* will move
7049 // the position by 32 bits not 24, leaving the upper 8 of those 32 bits
7050 // inaccessible. Avoid this by rounding up the size appropriately.
7051 FieldTy = IntegerType::get(Context, TD.getTypeAllocSizeInBits(FieldTy));
7053 assert(FieldTy->getPrimitiveSizeInBits() ==
7054 TD.getTypeAllocSizeInBits(FieldTy) && "Field type not sequential!");
7056 // If this is a bitfield, the field may span multiple fields in the LLVM
7057 // type. As such, cast the pointer to be a pointer to the declared type.
7058 FieldPtr = BitCastToType(FieldPtr, FieldTy->getPointerTo());
7060 unsigned LLVMValueBitSize = FieldTy->getPrimitiveSizeInBits();
7061 // Finally, because bitfields can span LLVM fields, and because the start
7062 // of the first LLVM field (where FieldPtr currently points) may be up to
7063 // 63 bits away from the start of the bitfield), it is possible that
7064 // *FieldPtr doesn't contain any of the bits for this bitfield. If needed,
7065 // adjust FieldPtr so that it is close enough to the bitfield that
7066 // *FieldPtr contains the first needed bit. Be careful to make sure that
7067 // the pointer remains appropriately aligned.
7068 if (BitStart > LLVMValueBitSize) {
7069 // In this case, we know that the alignment of the field is less than
7070 // the size of the field. To get the pointer close enough, add some
7071 // number of alignment units to the pointer.
7072 unsigned ByteAlignment = TD.getABITypeAlignment(FieldTy);
7073 // It is possible that an individual field is Packed. This information is
7074 // not reflected in FieldTy. Check DECL_PACKED here.
7075 if (DECL_PACKED(FieldDecl))
7076 ByteAlignment = 1;
7077 assert(ByteAlignment*8 <= LLVMValueBitSize && "Unknown overlap case!");
7078 unsigned NumAlignmentUnits = BitStart/(ByteAlignment*8);
7079 assert(NumAlignmentUnits && "Not adjusting pointer?");
7081 // Compute the byte offset, and add it to the pointer.
7082 unsigned ByteOffset = NumAlignmentUnits*ByteAlignment;
7083 LVAlign = MinAlign(LVAlign, ByteOffset);
7085 Constant *Offset = ConstantInt::get(TD.getIntPtrType(Context), ByteOffset);
7086 FieldPtr = CastToType(Instruction::PtrToInt, FieldPtr,
7087 Offset->getType());
7088 FieldPtr = Builder.CreateAdd(FieldPtr, Offset);
7089 FieldPtr = CastToType(Instruction::IntToPtr, FieldPtr,
7090 FieldTy->getPointerTo());
7092 // Adjust bitstart to account for the pointer movement.
7093 BitStart -= ByteOffset*8;
7095 // Check that this worked. Note that the bitfield may extend beyond
7096 // the end of *FieldPtr, for example because BitfieldSize is the same
7097 // as LLVMValueBitSize but BitStart > 0.
7098 assert(BitStart < LLVMValueBitSize &&
7099 BitStart+BitfieldSize < 2*LLVMValueBitSize &&
7100 "Couldn't get bitfield into value!");
7103 // Okay, everything is good. Return this as a bitfield if we can't
7104 // return it as a normal l-value. (e.g. "struct X { int X : 32 };" ).
7105 if (BitfieldSize != LLVMValueBitSize || BitStart != 0)
7106 return LValue(FieldPtr, LVAlign, BitStart, BitfieldSize);
7108 } else {
7109 // Make sure we return a pointer to the right type.
7110 const Type *EltTy = ConvertType(TREE_TYPE(exp));
7111 FieldPtr = BitCastToType(FieldPtr, EltTy->getPointerTo());
7114 assert(BitStart == 0 &&
7115 "It's a bitfield reference or we didn't get to the field!");
7116 return LValue(FieldPtr, LVAlign);
7119 LValue TreeToLLVM::EmitLV_DECL(tree exp) {
7120 if (TREE_CODE(exp) == PARM_DECL || TREE_CODE(exp) == VAR_DECL ||
7121 TREE_CODE(exp) == CONST_DECL) {
7122 // If a static var's type was incomplete when the decl was written,
7123 // but the type is complete now, lay out the decl now.
7124 if (DECL_SIZE(exp) == 0 && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P(TREE_TYPE(exp))
7125 && (TREE_STATIC(exp) || DECL_EXTERNAL(exp))) {
7126 layout_decl(exp, 0);
7128 #if 0
7129 // This mirrors code in layout_decl for munging the RTL. Here we actually
7130 // emit a NEW declaration for the global variable, now that it has been
7131 // laid out. We then tell the compiler to "forward" any uses of the old
7132 // global to this new one.
7133 if (Value *Val = DECL_LLVM_IF_SET(exp)) {
7134 //fprintf(stderr, "***\n*** SHOULD HANDLE GLOBAL VARIABLES!\n***\n");
7135 //assert(0 && "Reimplement this with replace all uses!");
7136 SET_DECL_LLVM(exp, 0);
7137 // Create a new global variable declaration
7138 llvm_assemble_external(exp);
7139 V2GV(Val)->ForwardedGlobal = V2GV(DECL_LLVM(exp));
7141 #endif
7145 assert(!isGimpleTemporary(exp) &&
7146 "Cannot use a gimple temporary as an l-value");
7148 Value *Decl = DECL_LLVM(exp);
7149 if (Decl == 0) {
7150 if (errorcount || sorrycount) {
7151 const Type *Ty = ConvertType(TREE_TYPE(exp));
7152 const PointerType *PTy = Ty->getPointerTo();
7153 LValue LV(ConstantPointerNull::get(PTy), 1);
7154 return LV;
7156 assert(0 && "INTERNAL ERROR: Referencing decl that hasn't been laid out");
7157 abort();
7160 // Ensure variable marked as used even if it doesn't go through a parser. If
7161 // it hasn't been used yet, write out an external definition.
7162 if (!TREE_USED(exp)) {
7163 assemble_external(exp);
7164 TREE_USED(exp) = 1;
7165 Decl = DECL_LLVM(exp);
7168 if (GlobalValue *GV = dyn_cast<GlobalValue>(Decl)) {
7169 // If this is an aggregate, emit it to LLVM now. GCC happens to
7170 // get this case right by forcing the initializer into memory.
7171 if (TREE_CODE(exp) == CONST_DECL || TREE_CODE(exp) == VAR_DECL) {
7172 if ((DECL_INITIAL(exp) || !TREE_PUBLIC(exp)) && !DECL_EXTERNAL(exp) &&
7173 GV->isDeclaration() &&
7174 !BOGUS_CTOR(exp)) {
7175 emit_global_to_llvm(exp);
7176 Decl = DECL_LLVM(exp); // Decl could have change if it changed type.
7178 } else {
7179 // Otherwise, inform cgraph that we used the global.
7180 mark_decl_referenced(exp);
7181 if (tree ID = DECL_ASSEMBLER_NAME(exp))
7182 mark_referenced(ID);
7186 const Type *Ty = ConvertType(TREE_TYPE(exp));
7187 // If we have "extern void foo", make the global have type {} instead of
7188 // type void.
7189 if (Ty->isVoidTy()) Ty = StructType::get(Context);
7190 const PointerType *PTy = Ty->getPointerTo();
7191 unsigned Alignment = Ty->isSized() ? TD.getABITypeAlignment(Ty) : 1;
7192 if (DECL_ALIGN(exp)) {
7193 if (DECL_USER_ALIGN(exp) || 8 * Alignment < (unsigned)DECL_ALIGN(exp))
7194 Alignment = DECL_ALIGN(exp) / 8;
7197 return LValue(BitCastToType(Decl, PTy), Alignment);
7200 LValue TreeToLLVM::EmitLV_EXC_PTR_EXPR(tree exp) {
7201 CreateExceptionValues();
7202 // Cast the address pointer to the expected type.
7203 unsigned Alignment = TD.getABITypeAlignment(cast<PointerType>(ExceptionValue->
7204 getType())->getElementType());
7205 return LValue(BitCastToType(ExceptionValue,
7206 ConvertType(TREE_TYPE(exp))->getPointerTo()),
7207 Alignment);
7210 LValue TreeToLLVM::EmitLV_FILTER_EXPR(tree exp) {
7211 CreateExceptionValues();
7212 unsigned Alignment =
7213 TD.getABITypeAlignment(cast<PointerType>(ExceptionSelectorValue->
7214 getType())->getElementType());
7215 return LValue(ExceptionSelectorValue, Alignment);
7218 LValue TreeToLLVM::EmitLV_INDIRECT_REF(tree exp) {
7219 // The lvalue is just the address.
7220 LValue LV = LValue(Emit(TREE_OPERAND(exp, 0), 0), expr_align(exp) / 8);
7221 // Correct for implicit type conversion: INDIRECT_REF can be applied to a
7222 // void*, resulting in a non-void type.
7223 LV.Ptr = BitCastToType(LV.Ptr, ConvertType(TREE_TYPE(exp))->getPointerTo());
7224 return LV;
7227 LValue TreeToLLVM::EmitLV_VIEW_CONVERT_EXPR(tree exp) {
7228 tree Op = TREE_OPERAND(exp, 0);
7230 if (isAggregateTreeType(TREE_TYPE(Op))) {
7231 // If the input is an aggregate, the address is the address of the operand.
7232 LValue LV = EmitLV(Op);
7233 // The type is the type of the expression.
7234 LV.Ptr = BitCastToType(LV.Ptr, ConvertType(TREE_TYPE(exp))->getPointerTo());
7235 return LV;
7236 } else {
7237 // If the input is a scalar, emit to a temporary.
7238 Value *Dest = CreateTemporary(ConvertType(TREE_TYPE(Op)));
7239 Builder.CreateStore(Emit(Op, 0), Dest);
7240 // The type is the type of the expression.
7241 Dest = BitCastToType(Dest, ConvertType(TREE_TYPE(exp))->getPointerTo());
7242 return LValue(Dest, 1);
7246 LValue TreeToLLVM::EmitLV_WITH_SIZE_EXPR(tree exp) {
7247 // The address is the address of the operand.
7248 return EmitLV(TREE_OPERAND(exp, 0));
7251 LValue TreeToLLVM::EmitLV_XXXXPART_EXPR(tree exp, unsigned Idx) {
7252 LValue Ptr = EmitLV(TREE_OPERAND(exp, 0));
7253 assert(!Ptr.isBitfield() &&
7254 "REALPART_EXPR / IMAGPART_EXPR operands cannot be bitfields!");
7255 unsigned Alignment;
7256 if (Idx == 0)
7257 // REALPART alignment is same as the complex operand.
7258 Alignment = Ptr.getAlignment();
7259 else
7260 // IMAGPART alignment = MinAlign(Ptr.Alignment, sizeof field);
7261 Alignment = MinAlign(Ptr.getAlignment(),
7262 TD.getTypeAllocSize(Ptr.Ptr->getType()));
7263 return LValue(Builder.CreateStructGEP(Ptr.Ptr, Idx), Alignment);
7266 Constant *TreeToLLVM::EmitLV_LABEL_DECL(tree exp) {
7267 return BlockAddress::get(Fn, getLabelDeclBlock(exp));
7270 //===----------------------------------------------------------------------===//
7271 // ... Constant Expressions ...
7272 //===----------------------------------------------------------------------===//
7274 /// EmitCONSTRUCTOR - emit the constructor into the location specified by
7275 /// DestLoc.
7276 Value *TreeToLLVM::EmitCONSTRUCTOR(tree exp, const MemRef *DestLoc) {
7277 tree type = TREE_TYPE(exp);
7278 const Type *Ty = ConvertType(type);
7279 if (const VectorType *PTy = dyn_cast<VectorType>(Ty)) {
7280 assert(DestLoc == 0 && "Dest location for packed value?");
7282 std::vector<Value *> BuildVecOps;
7284 // Insert zero initializers for any uninitialized values.
7285 Constant *Zero = Constant::getNullValue(PTy->getElementType());
7286 BuildVecOps.resize(cast<VectorType>(Ty)->getNumElements(), Zero);
7288 // Insert all of the elements here.
7289 unsigned HOST_WIDE_INT ix;
7290 tree purpose, value;
7291 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), ix, purpose, value) {
7292 if (!purpose) continue; // Not actually initialized?
7294 unsigned FieldNo = TREE_INT_CST_LOW(purpose);
7296 // Update the element.
7297 if (FieldNo < BuildVecOps.size())
7298 BuildVecOps[FieldNo] = Emit(value, 0);
7301 return BuildVector(BuildVecOps);
7304 assert(!Ty->isSingleValueType() && "Constructor for scalar type??");
7306 // Start out with the value zero'd out.
7307 EmitAggregateZero(*DestLoc, type);
7309 VEC(constructor_elt, gc) *elt = CONSTRUCTOR_ELTS(exp);
7310 switch (TREE_CODE(TREE_TYPE(exp))) {
7311 case ARRAY_TYPE:
7312 case RECORD_TYPE:
7313 default:
7314 if (elt && VEC_length(constructor_elt, elt)) {
7315 // We don't handle elements yet.
7317 TODO(exp);
7319 return 0;
7320 case QUAL_UNION_TYPE:
7321 case UNION_TYPE:
7322 // Store each element of the constructor into the corresponding field of
7323 // DEST.
7324 if (!elt || VEC_empty(constructor_elt, elt)) return 0; // no elements
7325 assert(VEC_length(constructor_elt, elt) == 1
7326 && "Union CONSTRUCTOR should have one element!");
7327 tree tree_purpose = VEC_index(constructor_elt, elt, 0)->index;
7328 tree tree_value = VEC_index(constructor_elt, elt, 0)->value;
7329 if (!tree_purpose)
7330 return 0; // Not actually initialized?
7332 if (!ConvertType(TREE_TYPE(tree_purpose))->isSingleValueType()) {
7333 Value *V = Emit(tree_value, DestLoc);
7334 assert(V == 0 && "Aggregate value returned in a register?");
7335 } else {
7336 // Scalar value. Evaluate to a register, then do the store.
7337 Value *V = Emit(tree_value, 0);
7338 Value *Ptr = BitCastToType(DestLoc->Ptr, V->getType()->getPointerTo());
7339 StoreInst *St = Builder.CreateStore(V, Ptr, DestLoc->Volatile);
7340 St->setAlignment(DestLoc->getAlignment());
7342 break;
7344 return 0;
7347 Constant *TreeConstantToLLVM::Convert(tree exp) {
7348 // Some front-ends use constants other than the standard language-independent
7349 // varieties, but which may still be output directly. Give the front-end a
7350 // chance to convert EXP to a language-independent representation.
7351 exp = lang_hooks.expand_constant (exp);
7353 assert((TREE_CONSTANT(exp) || TREE_CODE(exp) == STRING_CST) &&
7354 "Isn't a constant!");
7355 switch (TREE_CODE(exp)) {
7356 case FDESC_EXPR: // Needed on itanium
7357 default:
7358 debug_tree(exp);
7359 assert(0 && "Unknown constant to convert!");
7360 abort();
7361 case INTEGER_CST: return ConvertINTEGER_CST(exp);
7362 case REAL_CST: return ConvertREAL_CST(exp);
7363 case VECTOR_CST: return ConvertVECTOR_CST(exp);
7364 case STRING_CST: return ConvertSTRING_CST(exp);
7365 case COMPLEX_CST: return ConvertCOMPLEX_CST(exp);
7366 case NOP_EXPR: return ConvertNOP_EXPR(exp);
7367 case CONVERT_EXPR: return ConvertCONVERT_EXPR(exp);
7368 case PLUS_EXPR:
7369 case MINUS_EXPR: return ConvertBinOp_CST(exp);
7370 case CONSTRUCTOR: return ConvertCONSTRUCTOR(exp);
7371 case VIEW_CONVERT_EXPR: return Convert(TREE_OPERAND(exp, 0));
7372 case ADDR_EXPR:
7373 return TheFolder->CreateBitCast(EmitLV(TREE_OPERAND(exp, 0)),
7374 ConvertType(TREE_TYPE(exp)));
7378 Constant *TreeConstantToLLVM::ConvertINTEGER_CST(tree exp) {
7379 const Type *Ty = ConvertType(TREE_TYPE(exp));
7381 // Handle i128 specially.
7382 if (const IntegerType *IT = dyn_cast<IntegerType>(Ty)) {
7383 if (IT->getBitWidth() == 128) {
7384 // GCC only supports i128 on 64-bit systems.
7385 assert(HOST_BITS_PER_WIDE_INT == 64 &&
7386 "i128 only supported on 64-bit system");
7387 uint64_t Bits[] = { TREE_INT_CST_LOW(exp), TREE_INT_CST_HIGH(exp) };
7388 return ConstantInt::get(Context, APInt(128, 2, Bits));
7392 // Build the value as a ulong constant, then constant fold it to the right
7393 // type. This handles overflow and other things appropriately.
7394 uint64_t IntValue = getINTEGER_CSTVal(exp);
7395 ConstantInt *C = ConstantInt::get(Type::getInt64Ty(Context), IntValue);
7396 // The destination type can be a pointer, integer or floating point
7397 // so we need a generalized cast here
7398 Instruction::CastOps opcode = CastInst::getCastOpcode(C, false, Ty,
7399 !TYPE_UNSIGNED(TREE_TYPE(exp)));
7400 return TheFolder->CreateCast(opcode, C, Ty);
7403 Constant *TreeConstantToLLVM::ConvertREAL_CST(tree exp) {
7404 const Type *Ty = ConvertType(TREE_TYPE(exp));
7405 assert(Ty->isFloatingPoint() && "Integer REAL_CST?");
7406 long RealArr[2];
7407 union {
7408 int UArr[2];
7409 double V;
7411 if (Ty->isFloatTy() || Ty->isDoubleTy()) {
7412 REAL_VALUE_TO_TARGET_DOUBLE(TREE_REAL_CST(exp), RealArr);
7414 // Here's how this works:
7415 // REAL_VALUE_TO_TARGET_DOUBLE() will generate the floating point number
7416 // as an array of integers in the target's representation. Each integer
7417 // in the array will hold 32 bits of the result REGARDLESS OF THE HOST'S
7418 // INTEGER SIZE.
7420 // This, then, makes the conversion pretty simple. The tricky part is
7421 // getting the byte ordering correct and make sure you don't print any
7422 // more than 32 bits per integer on platforms with ints > 32 bits.
7424 // We want to switch the words of UArr if host and target endianness
7425 // do not match. FLOAT_WORDS_BIG_ENDIAN describes the target endianness.
7426 // The host's used to be available in HOST_WORDS_BIG_ENDIAN, but the gcc
7427 // maintainers removed this in a fit of cleanliness between 4.0
7428 // and 4.2. llvm::sys has a substitute.
7430 UArr[0] = RealArr[0]; // Long -> int convert
7431 UArr[1] = RealArr[1];
7433 if (llvm::sys::isBigEndianHost() != FLOAT_WORDS_BIG_ENDIAN)
7434 std::swap(UArr[0], UArr[1]);
7436 return
7437 ConstantFP::get(Context, Ty->isFloatTy() ?
7438 APFloat((float)V) : APFloat(V));
7439 } else if (Ty->isX86_FP80Ty()) {
7440 long RealArr[4];
7441 uint64_t UArr[2];
7442 REAL_VALUE_TO_TARGET_LONG_DOUBLE(TREE_REAL_CST(exp), RealArr);
7443 UArr[0] = ((uint64_t)((uint32_t)RealArr[0])) |
7444 ((uint64_t)((uint32_t)RealArr[1]) << 32);
7445 UArr[1] = (uint16_t)RealArr[2];
7446 return ConstantFP::get(Context, APFloat(APInt(80, 2, UArr)));
7447 } else if (Ty->isPPC_FP128Ty() ||
7448 Ty->isFP128Ty()) {
7449 long RealArr[4];
7450 uint64_t UArr[2];
7451 REAL_VALUE_TO_TARGET_LONG_DOUBLE(TREE_REAL_CST(exp), RealArr);
7453 UArr[0] = ((uint64_t)((uint32_t)RealArr[0]) << 32) |
7454 ((uint64_t)((uint32_t)RealArr[1]));
7455 UArr[1] = ((uint64_t)((uint32_t)RealArr[2]) << 32) |
7456 ((uint64_t)((uint32_t)RealArr[3]));
7457 return ConstantFP::get(Context,
7458 APFloat(APInt(128, 2, UArr),
7459 /*isIEEE*/ Ty->isFP128Ty()));
7461 assert(0 && "Floating point type not handled yet");
7462 return 0; // outwit compiler warning
7465 Constant *TreeConstantToLLVM::ConvertVECTOR_CST(tree exp) {
7466 if (!TREE_VECTOR_CST_ELTS(exp))
7467 return Constant::getNullValue(ConvertType(TREE_TYPE(exp)));
7469 std::vector<Constant*> Elts;
7470 for (tree elt = TREE_VECTOR_CST_ELTS(exp); elt; elt = TREE_CHAIN(elt))
7471 Elts.push_back(Convert(TREE_VALUE(elt)));
7473 // The vector should be zero filled if insufficient elements are provided.
7474 if (Elts.size() < TYPE_VECTOR_SUBPARTS(TREE_TYPE(exp))) {
7475 tree EltType = TREE_TYPE(TREE_TYPE(exp));
7476 Constant *Zero = Constant::getNullValue(ConvertType(EltType));
7477 while (Elts.size() < TYPE_VECTOR_SUBPARTS(TREE_TYPE(exp)))
7478 Elts.push_back(Zero);
7481 return ConstantVector::get(Elts);
7484 Constant *TreeConstantToLLVM::ConvertSTRING_CST(tree exp) {
7485 const ArrayType *StrTy = cast<ArrayType>(ConvertType(TREE_TYPE(exp)));
7486 const Type *ElTy = StrTy->getElementType();
7488 unsigned Len = (unsigned)TREE_STRING_LENGTH(exp);
7490 std::vector<Constant*> Elts;
7491 if (ElTy->isInteger(8)) {
7492 const unsigned char *InStr =(const unsigned char *)TREE_STRING_POINTER(exp);
7493 for (unsigned i = 0; i != Len; ++i)
7494 Elts.push_back(ConstantInt::get(Type::getInt8Ty(Context), InStr[i]));
7495 } else if (ElTy->isInteger(16)) {
7496 assert((Len&1) == 0 &&
7497 "Length in bytes should be a multiple of element size");
7498 const uint16_t *InStr =
7499 (const unsigned short *)TREE_STRING_POINTER(exp);
7500 for (unsigned i = 0; i != Len/2; ++i) {
7501 // gcc has constructed the initializer elements in the target endianness,
7502 // but we're going to treat them as ordinary shorts from here, with
7503 // host endianness. Adjust if necessary.
7504 if (llvm::sys::isBigEndianHost() == BYTES_BIG_ENDIAN)
7505 Elts.push_back(ConstantInt::get(Type::getInt16Ty(Context), InStr[i]));
7506 else
7507 Elts.push_back(ConstantInt::get(Type::getInt16Ty(Context), ByteSwap_16(InStr[i])));
7509 } else if (ElTy->isInteger(32)) {
7510 assert((Len&3) == 0 &&
7511 "Length in bytes should be a multiple of element size");
7512 const uint32_t *InStr = (const uint32_t *)TREE_STRING_POINTER(exp);
7513 for (unsigned i = 0; i != Len/4; ++i) {
7514 // gcc has constructed the initializer elements in the target endianness,
7515 // but we're going to treat them as ordinary ints from here, with
7516 // host endianness. Adjust if necessary.
7517 if (llvm::sys::isBigEndianHost() == BYTES_BIG_ENDIAN)
7518 Elts.push_back(ConstantInt::get(Type::getInt32Ty(Context), InStr[i]));
7519 else
7520 Elts.push_back(ConstantInt::get(Type::getInt32Ty(Context), ByteSwap_32(InStr[i])));
7522 } else {
7523 assert(0 && "Unknown character type!");
7526 unsigned LenInElts = Len /
7527 TREE_INT_CST_LOW(TYPE_SIZE_UNIT(TREE_TYPE(TREE_TYPE(exp))));
7528 unsigned ConstantSize = StrTy->getNumElements();
7530 if (LenInElts != ConstantSize) {
7531 // If this is a variable sized array type, set the length to LenInElts.
7532 if (ConstantSize == 0) {
7533 tree Domain = TYPE_DOMAIN(TREE_TYPE(exp));
7534 if (!Domain || !TYPE_MAX_VALUE(Domain)) {
7535 ConstantSize = LenInElts;
7536 StrTy = ArrayType::get(ElTy, LenInElts);
7540 if (ConstantSize < LenInElts) {
7541 // Only some chars are being used, truncate the string: char X[2] = "foo";
7542 Elts.resize(ConstantSize);
7543 } else {
7544 // Fill the end of the string with nulls.
7545 Constant *C = Constant::getNullValue(ElTy);
7546 for (; LenInElts != ConstantSize; ++LenInElts)
7547 Elts.push_back(C);
7550 return ConstantArray::get(StrTy, Elts);
7553 Constant *TreeConstantToLLVM::ConvertCOMPLEX_CST(tree exp) {
7554 std::vector<Constant*> Elts;
7555 Elts.push_back(Convert(TREE_REALPART(exp)));
7556 Elts.push_back(Convert(TREE_IMAGPART(exp)));
7557 return ConstantStruct::get(Context, Elts, false);
7560 Constant *TreeConstantToLLVM::ConvertNOP_EXPR(tree exp) {
7561 Constant *Elt = Convert(TREE_OPERAND(exp, 0));
7562 const Type *Ty = ConvertType(TREE_TYPE(exp));
7563 bool EltIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0)));
7564 bool TyIsSigned = !TYPE_UNSIGNED(TREE_TYPE(exp));
7566 // If this is a structure-to-structure cast, just return the uncasted value.
7567 if (!Elt->getType()->isSingleValueType() || !Ty->isSingleValueType())
7568 return Elt;
7570 // Elt and Ty can be integer, float or pointer here: need generalized cast
7571 Instruction::CastOps opcode = CastInst::getCastOpcode(Elt, EltIsSigned,
7572 Ty, TyIsSigned);
7573 return TheFolder->CreateCast(opcode, Elt, Ty);
7576 Constant *TreeConstantToLLVM::ConvertCONVERT_EXPR(tree exp) {
7577 Constant *Elt = Convert(TREE_OPERAND(exp, 0));
7578 bool EltIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp, 0)));
7579 const Type *Ty = ConvertType(TREE_TYPE(exp));
7580 bool TyIsSigned = !TYPE_UNSIGNED(TREE_TYPE(exp));
7581 Instruction::CastOps opcode = CastInst::getCastOpcode(Elt, EltIsSigned, Ty,
7582 TyIsSigned);
7583 return TheFolder->CreateCast(opcode, Elt, Ty);
7586 Constant *TreeConstantToLLVM::ConvertBinOp_CST(tree exp) {
7587 Constant *LHS = Convert(TREE_OPERAND(exp, 0));
7588 bool LHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp,0)));
7589 Constant *RHS = Convert(TREE_OPERAND(exp, 1));
7590 bool RHSIsSigned = !TYPE_UNSIGNED(TREE_TYPE(TREE_OPERAND(exp,1)));
7591 Instruction::CastOps opcode;
7592 if (isa<PointerType>(LHS->getType())) {
7593 const Type *IntPtrTy = getTargetData().getIntPtrType(Context);
7594 opcode = CastInst::getCastOpcode(LHS, LHSIsSigned, IntPtrTy, false);
7595 LHS = TheFolder->CreateCast(opcode, LHS, IntPtrTy);
7596 opcode = CastInst::getCastOpcode(RHS, RHSIsSigned, IntPtrTy, false);
7597 RHS = TheFolder->CreateCast(opcode, RHS, IntPtrTy);
7600 Constant *Result;
7601 switch (TREE_CODE(exp)) {
7602 default: assert(0 && "Unexpected case!");
7603 case PLUS_EXPR: Result = TheFolder->CreateAdd(LHS, RHS); break;
7604 case MINUS_EXPR: Result = TheFolder->CreateSub(LHS, RHS); break;
7607 const Type *Ty = ConvertType(TREE_TYPE(exp));
7608 bool TyIsSigned = !TYPE_UNSIGNED(TREE_TYPE(exp));
7609 opcode = CastInst::getCastOpcode(Result, LHSIsSigned, Ty, TyIsSigned);
7610 return TheFolder->CreateCast(opcode, Result, Ty);
7613 Constant *TreeConstantToLLVM::ConvertCONSTRUCTOR(tree exp) {
7614 // Please note, that we can have empty ctor, even if array is non-trivial (has
7615 // nonzero number of entries). This situation is typical for static ctors,
7616 // when array is filled during program initialization.
7617 if (CONSTRUCTOR_ELTS(exp) == 0 ||
7618 VEC_length(constructor_elt, CONSTRUCTOR_ELTS(exp)) == 0) // All zeros?
7619 return Constant::getNullValue(ConvertType(TREE_TYPE(exp)));
7621 switch (TREE_CODE(TREE_TYPE(exp))) {
7622 default:
7623 debug_tree(exp);
7624 assert(0 && "Unknown ctor!");
7625 case VECTOR_TYPE:
7626 case ARRAY_TYPE: return ConvertArrayCONSTRUCTOR(exp);
7627 case RECORD_TYPE: return ConvertRecordCONSTRUCTOR(exp);
7628 case QUAL_UNION_TYPE:
7629 case UNION_TYPE: return ConvertUnionCONSTRUCTOR(exp);
7633 Constant *TreeConstantToLLVM::ConvertArrayCONSTRUCTOR(tree exp) {
7634 // Vectors are like arrays, but the domain is stored via an array
7635 // type indirectly.
7637 // If we have a lower bound for the range of the type, get it.
7638 tree InitType = TREE_TYPE(exp);
7639 tree min_element = size_zero_node;
7640 std::vector<Constant*> ResultElts;
7642 if (TREE_CODE(InitType) == VECTOR_TYPE) {
7643 ResultElts.resize(TYPE_VECTOR_SUBPARTS(InitType));
7644 } else {
7645 assert(TREE_CODE(InitType) == ARRAY_TYPE && "Unknown type for init");
7646 tree Domain = TYPE_DOMAIN(InitType);
7647 if (Domain && TYPE_MIN_VALUE(Domain))
7648 min_element = fold_convert(sizetype, TYPE_MIN_VALUE(Domain));
7650 if (Domain && TYPE_MAX_VALUE(Domain)) {
7651 tree max_element = fold_convert(sizetype, TYPE_MAX_VALUE(Domain));
7652 tree size = size_binop (MINUS_EXPR, max_element, min_element);
7653 size = size_binop (PLUS_EXPR, size, size_one_node);
7655 if (host_integerp(size, 1))
7656 ResultElts.resize(tree_low_cst(size, 1));
7660 unsigned NextFieldToFill = 0;
7661 unsigned HOST_WIDE_INT ix;
7662 tree elt_index, elt_value;
7663 Constant *SomeVal = 0;
7664 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), ix, elt_index, elt_value) {
7665 // Find and decode the constructor's value.
7666 Constant *Val = Convert(elt_value);
7667 SomeVal = Val;
7669 // Get the index position of the element within the array. Note that this
7670 // can be NULL_TREE, which means that it belongs in the next available slot.
7671 tree index = elt_index;
7673 // The first and last field to fill in, inclusive.
7674 unsigned FieldOffset, FieldLastOffset;
7675 if (index && TREE_CODE(index) == RANGE_EXPR) {
7676 tree first = fold_convert (sizetype, TREE_OPERAND(index, 0));
7677 tree last = fold_convert (sizetype, TREE_OPERAND(index, 1));
7679 first = size_binop (MINUS_EXPR, first, min_element);
7680 last = size_binop (MINUS_EXPR, last, min_element);
7682 assert(host_integerp(first, 1) && host_integerp(last, 1) &&
7683 "Unknown range_expr!");
7684 FieldOffset = tree_low_cst(first, 1);
7685 FieldLastOffset = tree_low_cst(last, 1);
7686 } else if (index) {
7687 index = size_binop (MINUS_EXPR, fold_convert (sizetype, index),
7688 min_element);
7689 assert(host_integerp(index, 1));
7690 FieldOffset = tree_low_cst(index, 1);
7691 FieldLastOffset = FieldOffset;
7692 } else {
7693 FieldOffset = NextFieldToFill;
7694 FieldLastOffset = FieldOffset;
7697 // Process all of the elements in the range.
7698 for (--FieldOffset; FieldOffset != FieldLastOffset; ) {
7699 ++FieldOffset;
7700 if (FieldOffset == ResultElts.size())
7701 ResultElts.push_back(Val);
7702 else {
7703 if (FieldOffset >= ResultElts.size())
7704 ResultElts.resize(FieldOffset+1);
7705 ResultElts[FieldOffset] = Val;
7708 NextFieldToFill = FieldOffset+1;
7712 // Zero length array.
7713 if (ResultElts.empty())
7714 return ConstantArray::get(
7715 cast<ArrayType>(ConvertType(TREE_TYPE(exp))), ResultElts);
7716 assert(SomeVal && "If we had some initializer, we should have some value!");
7718 // Do a post-pass over all of the elements. We're taking care of two things
7719 // here:
7720 // #1. If any elements did not have initializers specified, provide them
7721 // with a null init.
7722 // #2. If any of the elements have different types, return a struct instead
7723 // of an array. This can occur in cases where we have an array of
7724 // unions, and the various unions had different pieces init'd.
7725 const Type *ElTy = SomeVal->getType();
7726 Constant *Filler = Constant::getNullValue(ElTy);
7727 bool AllEltsSameType = true;
7728 for (unsigned i = 0, e = ResultElts.size(); i != e; ++i) {
7729 if (ResultElts[i] == 0)
7730 ResultElts[i] = Filler;
7731 else if (ResultElts[i]->getType() != ElTy)
7732 AllEltsSameType = false;
7735 if (TREE_CODE(InitType) == VECTOR_TYPE) {
7736 assert(AllEltsSameType && "Vector of heterogeneous element types?");
7737 return ConstantVector::get(ResultElts);
7740 if (AllEltsSameType)
7741 return ConstantArray::get(
7742 ArrayType::get(ElTy, ResultElts.size()), ResultElts);
7743 return ConstantStruct::get(Context, ResultElts, false);
7747 namespace {
7748 /// ConstantLayoutInfo - A helper class used by ConvertRecordCONSTRUCTOR to
7749 /// lay out struct inits.
7750 struct ConstantLayoutInfo {
7751 const TargetData &TD;
7753 /// ResultElts - The initializer elements so far.
7754 std::vector<Constant*> ResultElts;
7756 /// StructIsPacked - This is set to true if we find out that we have to emit
7757 /// the ConstantStruct as a Packed LLVM struct type (because the LLVM
7758 /// alignment rules would prevent laying out the struct correctly).
7759 bool StructIsPacked;
7761 /// NextFieldByteStart - This field indicates the *byte* that the next field
7762 /// will start at. Put another way, this is the size of the struct as
7763 /// currently laid out, but without any tail padding considered.
7764 uint64_t NextFieldByteStart;
7766 /// MaxLLVMFieldAlignment - This is the largest alignment of any IR field,
7767 /// which is the alignment that the ConstantStruct will get.
7768 unsigned MaxLLVMFieldAlignment;
7771 ConstantLayoutInfo(const TargetData &TD) : TD(TD) {
7772 StructIsPacked = false;
7773 NextFieldByteStart = 0;
7774 MaxLLVMFieldAlignment = 1;
7777 void ConvertToPacked();
7778 void AddFieldToRecordConstant(Constant *Val, uint64_t GCCFieldOffsetInBits);
7779 void AddBitFieldToRecordConstant(ConstantInt *Val,
7780 uint64_t GCCFieldOffsetInBits);
7781 void HandleTailPadding(uint64_t GCCStructBitSize);
7786 /// ConvertToPacked - Given a partially constructed initializer for a LLVM
7787 /// struct constant, change it to make all the implicit padding between elements
7788 /// be fully explicit.
7789 void ConstantLayoutInfo::ConvertToPacked() {
7790 assert(!StructIsPacked && "Struct is already packed");
7791 uint64_t EltOffs = 0;
7792 for (unsigned i = 0, e = ResultElts.size(); i != e; ++i) {
7793 Constant *Val = ResultElts[i];
7795 // Check to see if this element has an alignment that would cause it to get
7796 // offset. If so, insert explicit padding for the offset.
7797 unsigned ValAlign = TD.getABITypeAlignment(Val->getType());
7798 uint64_t AlignedEltOffs = TargetData::RoundUpAlignment(EltOffs, ValAlign);
7800 // If the alignment doesn't affect the element offset, then the value is ok.
7801 // Accept the field and keep moving.
7802 if (AlignedEltOffs == EltOffs) {
7803 EltOffs += TD.getTypeAllocSize(Val->getType());
7804 continue;
7807 // Otherwise, there is padding here. Insert explicit zeros.
7808 const Type *PadTy = Type::getInt8Ty(Context);
7809 if (AlignedEltOffs-EltOffs != 1)
7810 PadTy = ArrayType::get(PadTy, AlignedEltOffs-EltOffs);
7811 ResultElts.insert(ResultElts.begin()+i,
7812 Constant::getNullValue(PadTy));
7814 // The padding is now element "i" and just bumped us up to "AlignedEltOffs".
7815 EltOffs = AlignedEltOffs;
7816 ++e; // One extra element to scan.
7819 // Packed now!
7820 MaxLLVMFieldAlignment = 1;
7821 StructIsPacked = true;
7825 /// AddFieldToRecordConstant - As ConvertRecordCONSTRUCTOR builds up an LLVM
7826 /// constant to represent a GCC CONSTRUCTOR node, it calls this method to add
7827 /// fields. The design of this is that it adds leading/trailing padding as
7828 /// needed to make the piece fit together and honor the GCC layout. This does
7829 /// not handle bitfields.
7831 /// The arguments are:
7832 /// Val: The value to add to the struct, with a size that matches the size of
7833 /// the corresponding GCC field.
7834 /// GCCFieldOffsetInBits: The offset that we have to put Val in the result.
7836 void ConstantLayoutInfo::
7837 AddFieldToRecordConstant(Constant *Val, uint64_t GCCFieldOffsetInBits) {
7838 // Figure out how to add this non-bitfield value to our constant struct so
7839 // that it ends up at the right offset. There are four cases we have to
7840 // think about:
7841 // 1. We may be able to just slap it onto the end of our struct and have
7842 // everything be ok.
7843 // 2. We may have to insert explicit padding into the LLVM struct to get
7844 // the initializer over into the right space. This is needed when the
7845 // GCC field has a larger alignment than the LLVM field.
7846 // 3. The LLVM field may be too far over and we may be forced to convert
7847 // this to an LLVM packed struct. This is required when the LLVM
7848 // alignment is larger than the GCC alignment.
7849 // 4. We may have a bitfield that needs to be merged into a previous
7850 // field.
7851 // Start by determining which case we have by looking at where LLVM and GCC
7852 // would place the field.
7854 // Verified that we haven't already laid out bytes that will overlap with
7855 // this new field.
7856 assert(NextFieldByteStart*8 <= GCCFieldOffsetInBits &&
7857 "Overlapping LLVM fields!");
7859 // Compute the offset the field would get if we just stuck 'Val' onto the
7860 // end of our structure right now. It is NextFieldByteStart rounded up to
7861 // the LLVM alignment of Val's type.
7862 unsigned ValLLVMAlign = 1;
7864 if (!StructIsPacked) { // Packed structs ignore the alignment of members.
7865 ValLLVMAlign = TD.getABITypeAlignment(Val->getType());
7866 MaxLLVMFieldAlignment = std::max(MaxLLVMFieldAlignment, ValLLVMAlign);
7869 // LLVMNaturalByteOffset - This is where LLVM would drop the field if we
7870 // slap it onto the end of the struct.
7871 uint64_t LLVMNaturalByteOffset
7872 = TargetData::RoundUpAlignment(NextFieldByteStart, ValLLVMAlign);
7874 // If adding the LLVM field would push it over too far, then we must have a
7875 // case that requires the LLVM struct to be packed. Do it now if so.
7876 if (LLVMNaturalByteOffset*8 > GCCFieldOffsetInBits) {
7877 // Switch to packed.
7878 ConvertToPacked();
7879 assert(NextFieldByteStart*8 <= GCCFieldOffsetInBits &&
7880 "Packing didn't fix the problem!");
7882 // Recurse to add the field after converting to packed.
7883 return AddFieldToRecordConstant(Val, GCCFieldOffsetInBits);
7886 // If the LLVM offset is not large enough, we need to insert explicit
7887 // padding in the LLVM struct between the fields.
7888 if (LLVMNaturalByteOffset*8 < GCCFieldOffsetInBits) {
7889 // Insert enough padding to fully fill in the hole. Insert padding from
7890 // NextFieldByteStart (not LLVMNaturalByteOffset) because the padding will
7891 // not get the same alignment as "Val".
7892 const Type *FillTy = Type::getInt8Ty(Context);
7893 if (GCCFieldOffsetInBits/8-NextFieldByteStart != 1)
7894 FillTy = ArrayType::get(FillTy,
7895 GCCFieldOffsetInBits/8-NextFieldByteStart);
7896 ResultElts.push_back(Constant::getNullValue(FillTy));
7898 NextFieldByteStart = GCCFieldOffsetInBits/8;
7900 // Recurse to add the field. This handles the case when the LLVM struct
7901 // needs to be converted to packed after inserting tail padding.
7902 return AddFieldToRecordConstant(Val, GCCFieldOffsetInBits);
7905 // Slap 'Val' onto the end of our ConstantStruct, it must be known to land
7906 // at the right offset now.
7907 assert(LLVMNaturalByteOffset*8 == GCCFieldOffsetInBits);
7908 ResultElts.push_back(Val);
7909 NextFieldByteStart = LLVMNaturalByteOffset;
7910 NextFieldByteStart += TD.getTypeAllocSize(Val->getType());
7913 /// AddBitFieldToRecordConstant - Bitfields can span multiple LLVM fields and
7914 /// have other annoying properties, thus requiring extra layout rules. This
7915 /// routine handles the extra complexity and then forwards to
7916 /// AddFieldToRecordConstant.
7917 void ConstantLayoutInfo::
7918 AddBitFieldToRecordConstant(ConstantInt *ValC, uint64_t GCCFieldOffsetInBits) {
7919 // If the GCC field starts after our current LLVM field then there must have
7920 // been an anonymous bitfield or other thing that shoved it over. No matter,
7921 // just insert some i8 padding until there are bits to fill in.
7922 while (GCCFieldOffsetInBits > NextFieldByteStart*8) {
7923 ResultElts.push_back(ConstantInt::get(Type::getInt8Ty(Context), 0));
7924 ++NextFieldByteStart;
7927 // If the field is a bitfield, it could partially go in a previously
7928 // laid out structure member, and may add elements to the end of the currently
7929 // laid out structure.
7931 // Since bitfields can only partially overlap other bitfields, because we
7932 // always emit components of bitfields as i8, and because we never emit tail
7933 // padding until we know it exists, this boils down to merging pieces of the
7934 // bitfield values into i8's. This is also simplified by the fact that
7935 // bitfields can only be initialized by ConstantInts. An interesting case is
7936 // sharing of tail padding in C++ structures. Because this can only happen
7937 // in inheritance cases, and those are non-POD, we should never see them here.
7939 // First handle any part of Val that overlaps an already laid out field by
7940 // merging it into it. By the above invariants, we know that it is an i8 that
7941 // we are merging into. Note that we may be inserting *all* of Val into the
7942 // previous field.
7943 if (GCCFieldOffsetInBits < NextFieldByteStart*8) {
7944 unsigned ValBitSize = ValC->getBitWidth();
7945 assert(!ResultElts.empty() && "Bitfield starts before first element?");
7946 assert(ResultElts.back()->getType()->isInteger(8) &&
7947 isa<ConstantInt>(ResultElts.back()) &&
7948 "Merging bitfield with non-bitfield value?");
7949 assert(NextFieldByteStart*8 - GCCFieldOffsetInBits < 8 &&
7950 "Bitfield overlaps backwards more than one field?");
7952 // Figure out how many bits can fit into the previous field given the
7953 // starting point in that field.
7954 unsigned BitsInPreviousField =
7955 unsigned(NextFieldByteStart*8 - GCCFieldOffsetInBits);
7956 assert(BitsInPreviousField != 0 && "Previous field should not be null!");
7958 // Split the bits that will be inserted into the previous element out of
7959 // Val into a new constant. If Val is completely contained in the previous
7960 // element, this sets Val to null, otherwise we shrink Val to contain the
7961 // bits to insert in the next element.
7962 APInt ValForPrevField(ValC->getValue());
7963 if (BitsInPreviousField >= ValBitSize) {
7964 // The whole field fits into the previous field.
7965 ValC = 0;
7966 } else if (!BYTES_BIG_ENDIAN) {
7967 // Little endian, take bits from the bottom of the field value.
7968 ValForPrevField.trunc(BitsInPreviousField);
7969 APInt Tmp = ValC->getValue();
7970 Tmp = Tmp.lshr(BitsInPreviousField);
7971 Tmp = Tmp.trunc(ValBitSize-BitsInPreviousField);
7972 ValC = ConstantInt::get(Context, Tmp);
7973 } else {
7974 // Big endian, take bits from the top of the field value.
7975 ValForPrevField = ValForPrevField.lshr(ValBitSize-BitsInPreviousField);
7976 ValForPrevField.trunc(BitsInPreviousField);
7978 APInt Tmp = ValC->getValue();
7979 Tmp = Tmp.trunc(ValBitSize-BitsInPreviousField);
7980 ValC = ConstantInt::get(Context, Tmp);
7983 // Okay, we're going to insert ValForPrevField into the previous i8, extend
7984 // it and shift into place.
7985 ValForPrevField.zext(8);
7986 if (!BYTES_BIG_ENDIAN) {
7987 ValForPrevField = ValForPrevField.shl(8-BitsInPreviousField);
7988 } else {
7989 // On big endian, if the entire field fits into the remaining space, shift
7990 // over to not take part of the next field's bits.
7991 if (BitsInPreviousField > ValBitSize)
7992 ValForPrevField = ValForPrevField.shl(BitsInPreviousField-ValBitSize);
7995 // "or" in the previous value and install it.
7996 const APInt &LastElt = cast<ConstantInt>(ResultElts.back())->getValue();
7997 ResultElts.back() = ConstantInt::get(Context, ValForPrevField | LastElt);
7999 // If the whole bit-field fit into the previous field, we're done.
8000 if (ValC == 0) return;
8001 GCCFieldOffsetInBits = NextFieldByteStart*8;
8004 APInt Val = ValC->getValue();
8006 // Okay, we know that we're plopping bytes onto the end of the struct.
8007 // Iterate while there is stuff to do.
8008 while (1) {
8009 ConstantInt *ValToAppend;
8010 if (Val.getBitWidth() > 8) {
8011 if (!BYTES_BIG_ENDIAN) {
8012 // Little endian lays out low bits first.
8013 APInt Tmp = Val;
8014 Tmp.trunc(8);
8015 ValToAppend = ConstantInt::get(Context, Tmp);
8017 Val = Val.lshr(8);
8018 } else {
8019 // Big endian lays out high bits first.
8020 APInt Tmp = Val;
8021 Tmp = Tmp.lshr(Tmp.getBitWidth()-8);
8022 Tmp.trunc(8);
8023 ValToAppend = ConstantInt::get(Context, Tmp);
8025 } else if (Val.getBitWidth() == 8) {
8026 ValToAppend = ConstantInt::get(Context, Val);
8027 } else {
8028 APInt Tmp = Val;
8029 Tmp.zext(8);
8031 if (BYTES_BIG_ENDIAN)
8032 Tmp = Tmp << 8-Val.getBitWidth();
8033 ValToAppend = ConstantInt::get(Context, Tmp);
8036 ResultElts.push_back(ValToAppend);
8037 ++NextFieldByteStart;
8039 if (Val.getBitWidth() <= 8)
8040 break;
8041 Val.trunc(Val.getBitWidth()-8);
8046 /// HandleTailPadding - Check to see if the struct fields, as laid out so far,
8047 /// will be large enough to make the generated constant struct have the right
8048 /// size. If not, add explicit tail padding. If rounding up based on the LLVM
8049 /// IR alignment would make the struct too large, convert it to a packed LLVM
8050 /// struct.
8051 void ConstantLayoutInfo::HandleTailPadding(uint64_t GCCStructBitSize) {
8052 uint64_t GCCStructSize = (GCCStructBitSize+7)/8;
8053 uint64_t LLVMNaturalSize =
8054 TargetData::RoundUpAlignment(NextFieldByteStart, MaxLLVMFieldAlignment);
8056 // If the total size of the laid out data is within the size of the GCC type
8057 // but the rounded-up size (including the tail padding induced by LLVM
8058 // alignment) is too big, convert to a packed struct type. We don't do this
8059 // if the size of the laid out fields is too large because initializers like
8061 // struct X { int A; char C[]; } x = { 4, "foo" };
8063 // can occur and no amount of packing will help.
8064 if (NextFieldByteStart <= GCCStructSize && // Not flexible init case.
8065 LLVMNaturalSize > GCCStructSize) { // Tail pad will overflow type.
8066 assert(!StructIsPacked && "LLVM Struct type overflow!");
8068 // Switch to packed.
8069 ConvertToPacked();
8070 LLVMNaturalSize = NextFieldByteStart;
8072 // Verify that packing solved the problem.
8073 assert(LLVMNaturalSize <= GCCStructSize &&
8074 "Oversized should be handled by packing");
8077 // If the LLVM Size is too small, add some tail padding to fill it in.
8078 if (LLVMNaturalSize < GCCStructSize) {
8079 const Type *FillTy = Type::getInt8Ty(Context);
8080 if (GCCStructSize - NextFieldByteStart != 1)
8081 FillTy = ArrayType::get(FillTy, GCCStructSize - NextFieldByteStart);
8082 ResultElts.push_back(Constant::getNullValue(FillTy));
8083 NextFieldByteStart = GCCStructSize;
8085 // At this point, we know that our struct should have the right size.
8086 // However, if the size of the struct is not a multiple of the largest
8087 // element alignment, the rounding could bump up the struct more. In this
8088 // case, we have to convert the struct to being packed.
8089 LLVMNaturalSize =
8090 TargetData::RoundUpAlignment(NextFieldByteStart, MaxLLVMFieldAlignment);
8092 // If the alignment will make the struct too big, convert it to being
8093 // packed.
8094 if (LLVMNaturalSize > GCCStructSize) {
8095 assert(!StructIsPacked && "LLVM Struct type overflow!");
8096 ConvertToPacked();
8101 Constant *TreeConstantToLLVM::ConvertRecordCONSTRUCTOR(tree exp) {
8102 ConstantLayoutInfo LayoutInfo(getTargetData());
8104 tree NextField = TYPE_FIELDS(TREE_TYPE(exp));
8105 unsigned HOST_WIDE_INT CtorIndex;
8106 tree FieldValue;
8107 tree Field; // The FIELD_DECL for the field.
8108 FOR_EACH_CONSTRUCTOR_ELT(CONSTRUCTOR_ELTS(exp), CtorIndex, Field, FieldValue){
8109 // If an explicit field is specified, use it.
8110 if (Field == 0) {
8111 Field = NextField;
8112 // Advance to the next FIELD_DECL, skipping over other structure members
8113 // (e.g. enums).
8114 while (1) {
8115 assert(Field && "Fell off end of record!");
8116 if (TREE_CODE(Field) == FIELD_DECL) break;
8117 Field = TREE_CHAIN(Field);
8121 // Decode the field's value.
8122 Constant *Val = Convert(FieldValue);
8124 // GCCFieldOffsetInBits is where GCC is telling us to put the current field.
8125 uint64_t GCCFieldOffsetInBits = getFieldOffsetInBits(Field);
8126 NextField = TREE_CHAIN(Field);
8128 uint64_t FieldSizeInBits = 0;
8129 if (DECL_SIZE(Field))
8130 FieldSizeInBits = getInt64(DECL_SIZE(Field), true);
8131 uint64_t ValueSizeInBits = Val->getType()->getPrimitiveSizeInBits();
8132 ConstantInt *ValC = dyn_cast<ConstantInt>(Val);
8133 if (ValC && ValC->isZero() && DECL_SIZE(Field)) {
8134 // G++ has various bugs handling {} initializers where it doesn't
8135 // synthesize a zero node of the right type. Instead of figuring out G++,
8136 // just hack around it by special casing zero and allowing it to be the
8137 // wrong size.
8138 if (ValueSizeInBits != FieldSizeInBits) {
8139 APInt ValAsInt = ValC->getValue();
8140 ValC = ConstantInt::get(Context, ValueSizeInBits < FieldSizeInBits ?
8141 ValAsInt.zext(FieldSizeInBits) :
8142 ValAsInt.trunc(FieldSizeInBits));
8143 ValueSizeInBits = FieldSizeInBits;
8144 Val = ValC;
8148 // If this is a non-bitfield value, just slap it onto the end of the struct
8149 // with the appropriate padding etc. If it is a bitfield, we have more
8150 // processing to do.
8151 if (!isBitfield(Field))
8152 LayoutInfo.AddFieldToRecordConstant(Val, GCCFieldOffsetInBits);
8153 else {
8154 // Bitfields can only be initialized with constants (integer constant
8155 // expressions).
8156 assert(ValC);
8157 assert(DECL_SIZE(Field));
8158 assert(ValueSizeInBits >= FieldSizeInBits &&
8159 "disagreement between LLVM and GCC on bitfield size");
8160 if (ValueSizeInBits != FieldSizeInBits) {
8161 // Fields are allowed to be smaller than their type. Simply discard
8162 // the unwanted upper bits in the field value.
8163 APInt ValAsInt = ValC->getValue();
8164 ValC = ConstantInt::get(Context, ValAsInt.trunc(FieldSizeInBits));
8166 LayoutInfo.AddBitFieldToRecordConstant(ValC, GCCFieldOffsetInBits);
8170 // Check to see if the struct fields, as laid out so far, will be large enough
8171 // to make the generated constant struct have the right size. If not, add
8172 // explicit tail padding. If rounding up based on the LLVM IR alignment would
8173 // make the struct too large, convert it to a packed LLVM struct.
8174 tree StructTypeSizeTree = TYPE_SIZE(TREE_TYPE(exp));
8175 if (StructTypeSizeTree && TREE_CODE(StructTypeSizeTree) == INTEGER_CST)
8176 LayoutInfo.HandleTailPadding(getInt64(StructTypeSizeTree, true));
8178 // Okay, we're done, return the computed elements.
8179 return ConstantStruct::get(Context, LayoutInfo.ResultElts,
8180 LayoutInfo.StructIsPacked);
8183 Constant *TreeConstantToLLVM::ConvertUnionCONSTRUCTOR(tree exp) {
8184 assert(!VEC_empty(constructor_elt, CONSTRUCTOR_ELTS(exp))
8185 && "Union CONSTRUCTOR has no elements? Zero?");
8187 VEC(constructor_elt, gc) *elt = CONSTRUCTOR_ELTS(exp);
8188 assert(VEC_length(constructor_elt, elt) == 1
8189 && "Union CONSTRUCTOR with multiple elements?");
8191 ConstantLayoutInfo LayoutInfo(getTargetData());
8193 // Convert the constant itself.
8194 Constant *Val = Convert(VEC_index(constructor_elt, elt, 0)->value);
8196 // Unions are initialized using the first member field. Find it.
8197 tree Field = TYPE_FIELDS(TREE_TYPE(exp));
8198 assert(Field && "cannot initialize union with no fields");
8199 while (TREE_CODE(Field) != FIELD_DECL) {
8200 Field = TREE_CHAIN(Field);
8201 assert(Field && "cannot initialize union with no fields");
8204 // If this is a non-bitfield value, just slap it onto the end of the struct
8205 // with the appropriate padding etc. If it is a bitfield, we have more
8206 // processing to do.
8207 if (!isBitfield(Field))
8208 LayoutInfo.AddFieldToRecordConstant(Val, 0);
8209 else {
8210 // Bitfields can only be initialized with constants (integer constant
8211 // expressions).
8212 ConstantInt *ValC = cast<ConstantInt>(Val);
8213 uint64_t FieldSizeInBits = getInt64(DECL_SIZE(Field), true);
8214 uint64_t ValueSizeInBits = Val->getType()->getPrimitiveSizeInBits();
8216 assert(ValueSizeInBits >= FieldSizeInBits &&
8217 "disagreement between LLVM and GCC on bitfield size");
8218 if (ValueSizeInBits != FieldSizeInBits) {
8219 // Fields are allowed to be smaller than their type. Simply discard
8220 // the unwanted upper bits in the field value.
8221 APInt ValAsInt = ValC->getValue();
8222 ValC = ConstantInt::get(Context, ValAsInt.trunc(FieldSizeInBits));
8224 LayoutInfo.AddBitFieldToRecordConstant(ValC, 0);
8227 // If the union has a fixed size, and if the value we converted isn't large
8228 // enough to fill all the bits, add a zero initialized array at the end to pad
8229 // it out.
8230 tree UnionTypeSizeTree = TYPE_SIZE(TREE_TYPE(exp));
8231 if (UnionTypeSizeTree && TREE_CODE(UnionTypeSizeTree) == INTEGER_CST)
8232 LayoutInfo.HandleTailPadding(getInt64(UnionTypeSizeTree, true));
8234 return ConstantStruct::get(Context, LayoutInfo.ResultElts,
8235 LayoutInfo.StructIsPacked);
8238 //===----------------------------------------------------------------------===//
8239 // ... Constant Expressions L-Values ...
8240 //===----------------------------------------------------------------------===//
8242 Constant *TreeConstantToLLVM::EmitLV(tree exp) {
8243 Constant *LV;
8245 switch (TREE_CODE(exp)) {
8246 default:
8247 debug_tree(exp);
8248 assert(0 && "Unknown constant lvalue to convert!");
8249 abort();
8250 case FUNCTION_DECL:
8251 case CONST_DECL:
8252 case VAR_DECL:
8253 LV = EmitLV_Decl(exp);
8254 break;
8255 case LABEL_DECL:
8256 LV = EmitLV_LABEL_DECL(exp);
8257 break;
8258 case COMPLEX_CST:
8259 LV = EmitLV_COMPLEX_CST(exp);
8260 break;
8261 case STRING_CST:
8262 LV = EmitLV_STRING_CST(exp);
8263 break;
8264 case COMPONENT_REF:
8265 LV = EmitLV_COMPONENT_REF(exp);
8266 break;
8267 case ARRAY_RANGE_REF:
8268 case ARRAY_REF:
8269 LV = EmitLV_ARRAY_REF(exp);
8270 break;
8271 case INDIRECT_REF:
8272 // The lvalue is just the address.
8273 LV = Convert(TREE_OPERAND(exp, 0));
8274 break;
8275 case COMPOUND_LITERAL_EXPR: // FIXME: not gimple - defined by C front-end
8276 /* This used to read
8277 return EmitLV(COMPOUND_LITERAL_EXPR_DECL(exp));
8278 but gcc warns about that and there doesn't seem to be any way to stop it
8279 with casts or the like. The following is equivalent with no checking
8280 (since we know TREE_CODE(exp) is COMPOUND_LITERAL_EXPR the checking
8281 doesn't accomplish anything anyway). */
8282 LV = EmitLV(DECL_EXPR_DECL (TREE_OPERAND (exp, 0)));
8283 break;
8286 // Check that the type of the lvalue is indeed that of a pointer to the tree
8287 // node. Since LLVM has no void* type, don't insist that void* be converted
8288 // to a specific LLVM type.
8289 assert((VOID_TYPE_P(TREE_TYPE(exp)) ||
8290 LV->getType() == ConvertType(TREE_TYPE(exp))->getPointerTo()) &&
8291 "LValue of constant has wrong type!");
8293 return LV;
8296 Constant *TreeConstantToLLVM::EmitLV_Decl(tree exp) {
8297 GlobalValue *Val = cast<GlobalValue>(DECL_LLVM(exp));
8299 // Ensure variable marked as used even if it doesn't go through a parser. If
8300 // it hasn't been used yet, write out an external definition.
8301 if (!TREE_USED(exp)) {
8302 assemble_external(exp);
8303 TREE_USED(exp) = 1;
8304 Val = cast<GlobalValue>(DECL_LLVM(exp));
8307 // If this is an aggregate, emit it to LLVM now. GCC happens to
8308 // get this case right by forcing the initializer into memory.
8309 if (TREE_CODE(exp) == CONST_DECL || TREE_CODE(exp) == VAR_DECL) {
8310 if ((DECL_INITIAL(exp) || !TREE_PUBLIC(exp)) && !DECL_EXTERNAL(exp) &&
8311 Val->isDeclaration() &&
8312 !BOGUS_CTOR(exp)) {
8313 emit_global_to_llvm(exp);
8314 // Decl could have change if it changed type.
8315 Val = cast<GlobalValue>(DECL_LLVM(exp));
8317 } else {
8318 // Otherwise, inform cgraph that we used the global.
8319 mark_decl_referenced(exp);
8320 if (tree ID = DECL_ASSEMBLER_NAME(exp))
8321 mark_referenced(ID);
8324 // The type of the global value output for exp need not match that of exp.
8325 // For example if the global's initializer has a different type to the global
8326 // itself (allowed in GCC but not in LLVM) then the global is changed to have
8327 // the type of the initializer. Correct for this now.
8328 const Type *Ty = ConvertType(TREE_TYPE(exp));
8329 if (Ty->isVoidTy()) Ty = Type::getInt8Ty(Context); // void* -> i8*.
8331 return TheFolder->CreateBitCast(Val, Ty->getPointerTo());
8334 /// EmitLV_LABEL_DECL - Someone took the address of a label.
8335 Constant *TreeConstantToLLVM::EmitLV_LABEL_DECL(tree exp) {
8336 assert(TheTreeToLLVM &&
8337 "taking the address of a label while not compiling the function!");
8339 // Figure out which function this is for, verify it's the one we're compiling.
8340 if (DECL_CONTEXT(exp)) {
8341 assert(TREE_CODE(DECL_CONTEXT(exp)) == FUNCTION_DECL &&
8342 "Address of label in nested function?");
8343 assert(TheTreeToLLVM->getFUNCTION_DECL() == DECL_CONTEXT(exp) &&
8344 "Taking the address of a label that isn't in the current fn!?");
8347 return TheTreeToLLVM->EmitLV_LABEL_DECL(exp);
8350 Constant *TreeConstantToLLVM::EmitLV_COMPLEX_CST(tree exp) {
8351 Constant *Init = TreeConstantToLLVM::ConvertCOMPLEX_CST(exp);
8353 // Cache the constants to avoid making obvious duplicates that have to be
8354 // folded by the optimizer.
8355 static std::map<Constant*, GlobalVariable*> ComplexCSTCache;
8356 GlobalVariable *&Slot = ComplexCSTCache[Init];
8357 if (Slot) return Slot;
8359 // Create a new complex global.
8360 Slot = new GlobalVariable(*TheModule, Init->getType(), true,
8361 GlobalVariable::PrivateLinkage, Init, ".cpx");
8362 return Slot;
8365 Constant *TreeConstantToLLVM::EmitLV_STRING_CST(tree exp) {
8366 Constant *Init = TreeConstantToLLVM::ConvertSTRING_CST(exp);
8368 // Support -fwritable-strings.
8369 bool StringIsConstant = !flag_writable_strings;
8371 GlobalVariable **SlotP = 0;
8373 if (StringIsConstant) {
8374 // Cache the string constants to avoid making obvious duplicate strings that
8375 // have to be folded by the optimizer.
8376 static std::map<Constant*, GlobalVariable*> StringCSTCache;
8377 GlobalVariable *&Slot = StringCSTCache[Init];
8378 if (Slot) return Slot;
8379 SlotP = &Slot;
8382 // Create a new string global.
8383 GlobalVariable *GV = new GlobalVariable(*TheModule, Init->getType(),
8384 StringIsConstant,
8385 GlobalVariable::PrivateLinkage, Init,
8386 ".str");
8387 GV->setAlignment(get_constant_alignment(exp) / 8);
8389 if (SlotP) *SlotP = GV;
8390 return GV;
8393 Constant *TreeConstantToLLVM::EmitLV_ARRAY_REF(tree exp) {
8394 tree Array = TREE_OPERAND(exp, 0);
8395 tree ArrayType = TREE_TYPE(Array);
8396 tree Index = TREE_OPERAND(exp, 1);
8397 tree IndexType = TREE_TYPE(Index);
8398 assert((TREE_CODE(ArrayType) == ARRAY_TYPE ||
8399 TREE_CODE(ArrayType) == POINTER_TYPE ||
8400 TREE_CODE(ArrayType) == REFERENCE_TYPE ||
8401 TREE_CODE(ArrayType) == BLOCK_POINTER_TYPE) &&
8402 "Unknown ARRAY_REF!");
8404 // Check for variable sized reference.
8405 // FIXME: add support for array types where the size doesn't fit into 64 bits
8406 assert(isSequentialCompatible(ArrayType) && "Global with variable size?");
8408 // As an LLVM extension, we allow ARRAY_REF with a pointer as the first
8409 // operand. This construct maps directly to a getelementptr instruction.
8410 Constant *ArrayAddr;
8411 if (TREE_CODE(ArrayType) == ARRAY_TYPE) {
8412 // First subtract the lower bound, if any, in the type of the index.
8413 tree LowerBound = array_ref_low_bound(exp);
8414 if (!integer_zerop(LowerBound))
8415 Index = fold(build2(MINUS_EXPR, IndexType, Index, LowerBound));
8416 ArrayAddr = EmitLV(Array);
8417 } else {
8418 ArrayAddr = Convert(Array);
8421 Constant *IndexVal = Convert(Index);
8423 const Type *IntPtrTy = getTargetData().getIntPtrType(Context);
8424 if (IndexVal->getType() != IntPtrTy)
8425 IndexVal = TheFolder->CreateIntCast(IndexVal, IntPtrTy,
8426 !TYPE_UNSIGNED(IndexType));
8428 std::vector<Value*> Idx;
8429 if (TREE_CODE(ArrayType) == ARRAY_TYPE)
8430 Idx.push_back(ConstantInt::get(IntPtrTy, 0));
8431 Idx.push_back(IndexVal);
8433 return TheFolder->CreateGetElementPtr(ArrayAddr, &Idx[0], Idx.size());
8436 Constant *TreeConstantToLLVM::EmitLV_COMPONENT_REF(tree exp) {
8437 Constant *StructAddrLV = EmitLV(TREE_OPERAND(exp, 0));
8439 // Ensure that the struct type has been converted, so that the fielddecls
8440 // are laid out.
8441 const Type *StructTy = ConvertType(TREE_TYPE(TREE_OPERAND(exp, 0)));
8443 tree FieldDecl = TREE_OPERAND(exp, 1);
8445 StructAddrLV = TheFolder->CreateBitCast(StructAddrLV,
8446 StructTy->getPointerTo());
8447 const Type *FieldTy = ConvertType(getDeclaredType(FieldDecl));
8449 // BitStart - This is the actual offset of the field from the start of the
8450 // struct, in bits. For bitfields this may be on a non-byte boundary.
8451 unsigned BitStart = getComponentRefOffsetInBits(exp);
8452 Constant *FieldPtr;
8453 const TargetData &TD = getTargetData();
8455 tree field_offset = component_ref_field_offset (exp);
8456 // If this is a normal field at a fixed offset from the start, handle it.
8457 if (TREE_CODE(field_offset) == INTEGER_CST) {
8458 unsigned int MemberIndex = GET_LLVM_FIELD_INDEX(FieldDecl);
8460 Constant *Ops[] = {
8461 StructAddrLV,
8462 Constant::getNullValue(Type::getInt32Ty(Context)),
8463 ConstantInt::get(Type::getInt32Ty(Context), MemberIndex)
8465 FieldPtr = TheFolder->CreateGetElementPtr(StructAddrLV, Ops+1, 2);
8467 FieldPtr = ConstantFoldInstOperands(Instruction::GetElementPtr,
8468 FieldPtr->getType(), Ops, 3, &TD);
8470 // Now that we did an offset from the start of the struct, subtract off
8471 // the offset from BitStart.
8472 if (MemberIndex) {
8473 const StructLayout *SL = TD.getStructLayout(cast<StructType>(StructTy));
8474 BitStart -= SL->getElementOffset(MemberIndex) * 8;
8477 } else {
8478 Constant *Offset = Convert(field_offset);
8479 Constant *Ptr = TheFolder->CreatePtrToInt(StructAddrLV, Offset->getType());
8480 Ptr = TheFolder->CreateAdd(Ptr, Offset);
8481 FieldPtr = TheFolder->CreateIntToPtr(Ptr,
8482 FieldTy->getPointerTo());
8485 // Make sure we return a result of the right type.
8486 if (FieldTy->getPointerTo() != FieldPtr->getType())
8487 FieldPtr = TheFolder->CreateBitCast(FieldPtr, FieldTy->getPointerTo());
8489 assert(BitStart == 0 &&
8490 "It's a bitfield reference or we didn't get to the field!");
8491 return FieldPtr;
8494 /* LLVM LOCAL end (ENTIRE FILE!) */