1 //===-- Writer.cpp - Library for converting LLVM code to C ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This library converts LLVM code to C code, compilable by GCC and other C
13 //===----------------------------------------------------------------------===//
15 #include "CTargetMachine.h"
16 #include "llvm/CallingConv.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Pass.h"
22 #include "llvm/PassManager.h"
23 #include "llvm/SymbolTable.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/IntrinsicInst.h"
26 #include "llvm/Analysis/ConstantsScanner.h"
27 #include "llvm/Analysis/FindUsedTypes.h"
28 #include "llvm/Analysis/LoopInfo.h"
29 #include "llvm/CodeGen/IntrinsicLowering.h"
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Target/TargetMachineRegistry.h"
32 #include "llvm/Support/CallSite.h"
33 #include "llvm/Support/CFG.h"
34 #include "llvm/Support/GetElementPtrTypeIterator.h"
35 #include "llvm/Support/InstVisitor.h"
36 #include "llvm/Support/Mangler.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/ADT/STLExtras.h"
40 #include "llvm/Support/MathExtras.h"
41 #include "llvm/Config/config.h"
49 // Register the target.
50 RegisterTarget<CTargetMachine> X("c", " C backend");
52 /// CBackendNameAllUsedStructsAndMergeFunctions - This pass inserts names for
53 /// any unnamed structure types that are used by the program, and merges
54 /// external functions with the same name.
56 class CBackendNameAllUsedStructsAndMergeFunctions : public ModulePass {
57 void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.addRequired<FindUsedTypes>();
61 virtual const char *getPassName() const {
62 return "C backend type canonicalizer";
65 virtual bool runOnModule(Module &M);
68 /// CWriter - This class is the main chunk of code that converts an LLVM
69 /// module to a C translation unit.
70 class CWriter : public FunctionPass, public InstVisitor<CWriter> {
72 DefaultIntrinsicLowering IL;
75 const Module *TheModule;
76 std::map<const Type *, std::string> TypeNames;
78 std::map<const ConstantFP *, unsigned> FPConstantMap;
80 CWriter(std::ostream &o) : Out(o) {}
82 virtual const char *getPassName() const { return "C backend"; }
84 void getAnalysisUsage(AnalysisUsage &AU) const {
85 AU.addRequired<LoopInfo>();
89 virtual bool doInitialization(Module &M);
91 bool runOnFunction(Function &F) {
92 LI = &getAnalysis<LoopInfo>();
94 // Get rid of intrinsics we can't handle.
97 // Output all floating point constants that cannot be printed accurately.
98 printFloatingPointConstants(F);
100 // Ensure that no local symbols conflict with global symbols.
101 F.renameLocalSymbols();
104 FPConstantMap.clear();
108 virtual bool doFinalization(Module &M) {
115 std::ostream &printType(std::ostream &Out, const Type *Ty,
116 const std::string &VariableName = "",
117 bool IgnoreName = false);
119 void printStructReturnPointerFunctionType(std::ostream &Out,
120 const PointerType *Ty);
122 void writeOperand(Value *Operand);
123 void writeOperandInternal(Value *Operand);
124 void writeOperandWithCast(Value* Operand, unsigned Opcode);
125 bool writeInstructionCast(const Instruction &I);
128 void lowerIntrinsics(Function &F);
130 void printModule(Module *M);
131 void printModuleTypes(const SymbolTable &ST);
132 void printContainedStructs(const Type *Ty, std::set<const StructType *> &);
133 void printFloatingPointConstants(Function &F);
134 void printFunctionSignature(const Function *F, bool Prototype);
136 void printFunction(Function &);
137 void printBasicBlock(BasicBlock *BB);
138 void printLoop(Loop *L);
140 void printConstant(Constant *CPV);
141 void printConstantWithCast(Constant *CPV, unsigned Opcode);
142 bool printConstExprCast(const ConstantExpr *CE);
143 void printConstantArray(ConstantArray *CPA);
144 void printConstantPacked(ConstantPacked *CP);
146 // isInlinableInst - Attempt to inline instructions into their uses to build
147 // trees as much as possible. To do this, we have to consistently decide
148 // what is acceptable to inline, so that variable declarations don't get
149 // printed and an extra copy of the expr is not emitted.
151 static bool isInlinableInst(const Instruction &I) {
152 // Always inline setcc instructions, even if they are shared by multiple
153 // expressions. GCC generates horrible code if we don't.
154 if (isa<SetCondInst>(I)) return true;
156 // Must be an expression, must be used exactly once. If it is dead, we
157 // emit it inline where it would go.
158 if (I.getType() == Type::VoidTy || !I.hasOneUse() ||
159 isa<TerminatorInst>(I) || isa<CallInst>(I) || isa<PHINode>(I) ||
160 isa<LoadInst>(I) || isa<VAArgInst>(I))
161 // Don't inline a load across a store or other bad things!
164 // Only inline instruction it it's use is in the same BB as the inst.
165 return I.getParent() == cast<Instruction>(I.use_back())->getParent();
168 // isDirectAlloca - Define fixed sized allocas in the entry block as direct
169 // variables which are accessed with the & operator. This causes GCC to
170 // generate significantly better code than to emit alloca calls directly.
172 static const AllocaInst *isDirectAlloca(const Value *V) {
173 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
174 if (!AI) return false;
175 if (AI->isArrayAllocation())
176 return 0; // FIXME: we can also inline fixed size array allocas!
177 if (AI->getParent() != &AI->getParent()->getParent()->getEntryBlock())
182 // Instruction visitation functions
183 friend class InstVisitor<CWriter>;
185 void visitReturnInst(ReturnInst &I);
186 void visitBranchInst(BranchInst &I);
187 void visitSwitchInst(SwitchInst &I);
188 void visitInvokeInst(InvokeInst &I) {
189 assert(0 && "Lowerinvoke pass didn't work!");
192 void visitUnwindInst(UnwindInst &I) {
193 assert(0 && "Lowerinvoke pass didn't work!");
195 void visitUnreachableInst(UnreachableInst &I);
197 void visitPHINode(PHINode &I);
198 void visitBinaryOperator(Instruction &I);
200 void visitCastInst (CastInst &I);
201 void visitSelectInst(SelectInst &I);
202 void visitCallInst (CallInst &I);
203 void visitShiftInst(ShiftInst &I) { visitBinaryOperator(I); }
205 void visitMallocInst(MallocInst &I);
206 void visitAllocaInst(AllocaInst &I);
207 void visitFreeInst (FreeInst &I);
208 void visitLoadInst (LoadInst &I);
209 void visitStoreInst (StoreInst &I);
210 void visitGetElementPtrInst(GetElementPtrInst &I);
211 void visitVAArgInst (VAArgInst &I);
213 void visitInstruction(Instruction &I) {
214 std::cerr << "C Writer does not know about " << I;
218 void outputLValue(Instruction *I) {
219 Out << " " << Mang->getValueName(I) << " = ";
222 bool isGotoCodeNecessary(BasicBlock *From, BasicBlock *To);
223 void printPHICopiesForSuccessor(BasicBlock *CurBlock,
224 BasicBlock *Successor, unsigned Indent);
225 void printBranchToBlock(BasicBlock *CurBlock, BasicBlock *SuccBlock,
227 void printIndexingExpression(Value *Ptr, gep_type_iterator I,
228 gep_type_iterator E);
232 /// This method inserts names for any unnamed structure types that are used by
233 /// the program, and removes names from structure types that are not used by the
236 bool CBackendNameAllUsedStructsAndMergeFunctions::runOnModule(Module &M) {
237 // Get a set of types that are used by the program...
238 std::set<const Type *> UT = getAnalysis<FindUsedTypes>().getTypes();
240 // Loop over the module symbol table, removing types from UT that are
241 // already named, and removing names for types that are not used.
243 SymbolTable &MST = M.getSymbolTable();
244 for (SymbolTable::type_iterator TI = MST.type_begin(), TE = MST.type_end();
246 SymbolTable::type_iterator I = TI++;
248 // If this is not used, remove it from the symbol table.
249 std::set<const Type *>::iterator UTI = UT.find(I->second);
253 UT.erase(UTI); // Only keep one name for this type.
256 // UT now contains types that are not named. Loop over it, naming
259 bool Changed = false;
260 unsigned RenameCounter = 0;
261 for (std::set<const Type *>::const_iterator I = UT.begin(), E = UT.end();
263 if (const StructType *ST = dyn_cast<StructType>(*I)) {
264 while (M.addTypeName("unnamed"+utostr(RenameCounter), ST))
270 // Loop over all external functions and globals. If we have two with
271 // identical names, merge them.
272 // FIXME: This code should disappear when we don't allow values with the same
273 // names when they have different types!
274 std::map<std::string, GlobalValue*> ExtSymbols;
275 for (Module::iterator I = M.begin(), E = M.end(); I != E;) {
277 if (GV->isExternal() && GV->hasName()) {
278 std::pair<std::map<std::string, GlobalValue*>::iterator, bool> X
279 = ExtSymbols.insert(std::make_pair(GV->getName(), GV));
281 // Found a conflict, replace this global with the previous one.
282 GlobalValue *OldGV = X.first->second;
283 GV->replaceAllUsesWith(ConstantExpr::getCast(OldGV, GV->getType()));
284 GV->eraseFromParent();
289 // Do the same for globals.
290 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
292 GlobalVariable *GV = I++;
293 if (GV->isExternal() && GV->hasName()) {
294 std::pair<std::map<std::string, GlobalValue*>::iterator, bool> X
295 = ExtSymbols.insert(std::make_pair(GV->getName(), GV));
297 // Found a conflict, replace this global with the previous one.
298 GlobalValue *OldGV = X.first->second;
299 GV->replaceAllUsesWith(ConstantExpr::getCast(OldGV, GV->getType()));
300 GV->eraseFromParent();
309 /// printStructReturnPointerFunctionType - This is like printType for a struct
310 /// return type, except, instead of printing the type as void (*)(Struct*, ...)
311 /// print it as "Struct (*)(...)", for struct return functions.
312 void CWriter::printStructReturnPointerFunctionType(std::ostream &Out,
313 const PointerType *TheTy) {
314 const FunctionType *FTy = cast<FunctionType>(TheTy->getElementType());
315 std::stringstream FunctionInnards;
316 FunctionInnards << " (*) (";
317 bool PrintedType = false;
319 FunctionType::param_iterator I = FTy->param_begin(), E = FTy->param_end();
320 const Type *RetTy = cast<PointerType>(I->get())->getElementType();
321 for (++I; I != E; ++I) {
323 FunctionInnards << ", ";
324 printType(FunctionInnards, *I, "");
327 if (FTy->isVarArg()) {
329 FunctionInnards << ", ...";
330 } else if (!PrintedType) {
331 FunctionInnards << "void";
333 FunctionInnards << ')';
334 std::string tstr = FunctionInnards.str();
335 printType(Out, RetTy, tstr);
339 // Pass the Type* and the variable name and this prints out the variable
342 std::ostream &CWriter::printType(std::ostream &Out, const Type *Ty,
343 const std::string &NameSoFar,
345 if (Ty->isPrimitiveType())
346 switch (Ty->getTypeID()) {
347 case Type::VoidTyID: return Out << "void " << NameSoFar;
348 case Type::BoolTyID: return Out << "bool " << NameSoFar;
349 case Type::UByteTyID: return Out << "unsigned char " << NameSoFar;
350 case Type::SByteTyID: return Out << "signed char " << NameSoFar;
351 case Type::UShortTyID: return Out << "unsigned short " << NameSoFar;
352 case Type::ShortTyID: return Out << "short " << NameSoFar;
353 case Type::UIntTyID: return Out << "unsigned " << NameSoFar;
354 case Type::IntTyID: return Out << "int " << NameSoFar;
355 case Type::ULongTyID: return Out << "unsigned long long " << NameSoFar;
356 case Type::LongTyID: return Out << "signed long long " << NameSoFar;
357 case Type::FloatTyID: return Out << "float " << NameSoFar;
358 case Type::DoubleTyID: return Out << "double " << NameSoFar;
360 std::cerr << "Unknown primitive type: " << *Ty << "\n";
364 // Check to see if the type is named.
365 if (!IgnoreName || isa<OpaqueType>(Ty)) {
366 std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
367 if (I != TypeNames.end()) return Out << I->second << ' ' << NameSoFar;
370 switch (Ty->getTypeID()) {
371 case Type::FunctionTyID: {
372 const FunctionType *FTy = cast<FunctionType>(Ty);
373 std::stringstream FunctionInnards;
374 FunctionInnards << " (" << NameSoFar << ") (";
375 for (FunctionType::param_iterator I = FTy->param_begin(),
376 E = FTy->param_end(); I != E; ++I) {
377 if (I != FTy->param_begin())
378 FunctionInnards << ", ";
379 printType(FunctionInnards, *I, "");
381 if (FTy->isVarArg()) {
382 if (FTy->getNumParams())
383 FunctionInnards << ", ...";
384 } else if (!FTy->getNumParams()) {
385 FunctionInnards << "void";
387 FunctionInnards << ')';
388 std::string tstr = FunctionInnards.str();
389 printType(Out, FTy->getReturnType(), tstr);
392 case Type::StructTyID: {
393 const StructType *STy = cast<StructType>(Ty);
394 Out << NameSoFar + " {\n";
396 for (StructType::element_iterator I = STy->element_begin(),
397 E = STy->element_end(); I != E; ++I) {
399 printType(Out, *I, "field" + utostr(Idx++));
405 case Type::PointerTyID: {
406 const PointerType *PTy = cast<PointerType>(Ty);
407 std::string ptrName = "*" + NameSoFar;
409 if (isa<ArrayType>(PTy->getElementType()) ||
410 isa<PackedType>(PTy->getElementType()))
411 ptrName = "(" + ptrName + ")";
413 return printType(Out, PTy->getElementType(), ptrName);
416 case Type::ArrayTyID: {
417 const ArrayType *ATy = cast<ArrayType>(Ty);
418 unsigned NumElements = ATy->getNumElements();
419 if (NumElements == 0) NumElements = 1;
420 return printType(Out, ATy->getElementType(),
421 NameSoFar + "[" + utostr(NumElements) + "]");
424 case Type::PackedTyID: {
425 const PackedType *PTy = cast<PackedType>(Ty);
426 unsigned NumElements = PTy->getNumElements();
427 if (NumElements == 0) NumElements = 1;
428 return printType(Out, PTy->getElementType(),
429 NameSoFar + "[" + utostr(NumElements) + "]");
432 case Type::OpaqueTyID: {
433 static int Count = 0;
434 std::string TyName = "struct opaque_" + itostr(Count++);
435 assert(TypeNames.find(Ty) == TypeNames.end());
436 TypeNames[Ty] = TyName;
437 return Out << TyName << ' ' << NameSoFar;
440 assert(0 && "Unhandled case in getTypeProps!");
447 void CWriter::printConstantArray(ConstantArray *CPA) {
449 // As a special case, print the array as a string if it is an array of
450 // ubytes or an array of sbytes with positive values.
452 const Type *ETy = CPA->getType()->getElementType();
453 bool isString = (ETy == Type::SByteTy || ETy == Type::UByteTy);
455 // Make sure the last character is a null char, as automatically added by C
456 if (isString && (CPA->getNumOperands() == 0 ||
457 !cast<Constant>(*(CPA->op_end()-1))->isNullValue()))
462 // Keep track of whether the last number was a hexadecimal escape
463 bool LastWasHex = false;
465 // Do not include the last character, which we know is null
466 for (unsigned i = 0, e = CPA->getNumOperands()-1; i != e; ++i) {
467 unsigned char C = cast<ConstantInt>(CPA->getOperand(i))->getZExtValue();
469 // Print it out literally if it is a printable character. The only thing
470 // to be careful about is when the last letter output was a hex escape
471 // code, in which case we have to be careful not to print out hex digits
472 // explicitly (the C compiler thinks it is a continuation of the previous
473 // character, sheesh...)
475 if (isprint(C) && (!LastWasHex || !isxdigit(C))) {
477 if (C == '"' || C == '\\')
484 case '\n': Out << "\\n"; break;
485 case '\t': Out << "\\t"; break;
486 case '\r': Out << "\\r"; break;
487 case '\v': Out << "\\v"; break;
488 case '\a': Out << "\\a"; break;
489 case '\"': Out << "\\\""; break;
490 case '\'': Out << "\\\'"; break;
493 Out << (char)(( C/16 < 10) ? ( C/16 +'0') : ( C/16 -10+'A'));
494 Out << (char)(((C&15) < 10) ? ((C&15)+'0') : ((C&15)-10+'A'));
503 if (CPA->getNumOperands()) {
505 printConstant(cast<Constant>(CPA->getOperand(0)));
506 for (unsigned i = 1, e = CPA->getNumOperands(); i != e; ++i) {
508 printConstant(cast<Constant>(CPA->getOperand(i)));
515 void CWriter::printConstantPacked(ConstantPacked *CP) {
517 if (CP->getNumOperands()) {
519 printConstant(cast<Constant>(CP->getOperand(0)));
520 for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
522 printConstant(cast<Constant>(CP->getOperand(i)));
528 // isFPCSafeToPrint - Returns true if we may assume that CFP may be written out
529 // textually as a double (rather than as a reference to a stack-allocated
530 // variable). We decide this by converting CFP to a string and back into a
531 // double, and then checking whether the conversion results in a bit-equal
532 // double to the original value of CFP. This depends on us and the target C
533 // compiler agreeing on the conversion process (which is pretty likely since we
534 // only deal in IEEE FP).
536 static bool isFPCSafeToPrint(const ConstantFP *CFP) {
537 #if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
539 sprintf(Buffer, "%a", CFP->getValue());
541 if (!strncmp(Buffer, "0x", 2) ||
542 !strncmp(Buffer, "-0x", 3) ||
543 !strncmp(Buffer, "+0x", 3))
544 return atof(Buffer) == CFP->getValue();
547 std::string StrVal = ftostr(CFP->getValue());
549 while (StrVal[0] == ' ')
550 StrVal.erase(StrVal.begin());
552 // Check to make sure that the stringized number is not some string like "Inf"
553 // or NaN. Check that the string matches the "[-+]?[0-9]" regex.
554 if ((StrVal[0] >= '0' && StrVal[0] <= '9') ||
555 ((StrVal[0] == '-' || StrVal[0] == '+') &&
556 (StrVal[1] >= '0' && StrVal[1] <= '9')))
557 // Reparse stringized version!
558 return atof(StrVal.c_str()) == CFP->getValue();
563 // printConstant - The LLVM Constant to C Constant converter.
564 void CWriter::printConstant(Constant *CPV) {
565 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CPV)) {
566 switch (CE->getOpcode()) {
567 case Instruction::Cast:
569 printType(Out, CPV->getType());
571 printConstant(CE->getOperand(0));
575 case Instruction::GetElementPtr:
577 printIndexingExpression(CE->getOperand(0), gep_type_begin(CPV),
581 case Instruction::Select:
583 printConstant(CE->getOperand(0));
585 printConstant(CE->getOperand(1));
587 printConstant(CE->getOperand(2));
590 case Instruction::Add:
591 case Instruction::Sub:
592 case Instruction::Mul:
593 case Instruction::SDiv:
594 case Instruction::UDiv:
595 case Instruction::FDiv:
596 case Instruction::URem:
597 case Instruction::SRem:
598 case Instruction::FRem:
599 case Instruction::And:
600 case Instruction::Or:
601 case Instruction::Xor:
602 case Instruction::SetEQ:
603 case Instruction::SetNE:
604 case Instruction::SetLT:
605 case Instruction::SetLE:
606 case Instruction::SetGT:
607 case Instruction::SetGE:
608 case Instruction::Shl:
609 case Instruction::LShr:
610 case Instruction::AShr:
613 bool NeedsClosingParens = printConstExprCast(CE);
614 printConstantWithCast(CE->getOperand(0), CE->getOpcode());
615 switch (CE->getOpcode()) {
616 case Instruction::Add: Out << " + "; break;
617 case Instruction::Sub: Out << " - "; break;
618 case Instruction::Mul: Out << " * "; break;
619 case Instruction::URem:
620 case Instruction::SRem:
621 case Instruction::FRem: Out << " % "; break;
622 case Instruction::UDiv:
623 case Instruction::SDiv:
624 case Instruction::FDiv: Out << " / "; break;
625 case Instruction::And: Out << " & "; break;
626 case Instruction::Or: Out << " | "; break;
627 case Instruction::Xor: Out << " ^ "; break;
628 case Instruction::SetEQ: Out << " == "; break;
629 case Instruction::SetNE: Out << " != "; break;
630 case Instruction::SetLT: Out << " < "; break;
631 case Instruction::SetLE: Out << " <= "; break;
632 case Instruction::SetGT: Out << " > "; break;
633 case Instruction::SetGE: Out << " >= "; break;
634 case Instruction::Shl: Out << " << "; break;
635 case Instruction::LShr:
636 case Instruction::AShr: Out << " >> "; break;
637 default: assert(0 && "Illegal opcode here!");
639 printConstantWithCast(CE->getOperand(1), CE->getOpcode());
640 if (NeedsClosingParens)
647 std::cerr << "CWriter Error: Unhandled constant expression: "
651 } else if (isa<UndefValue>(CPV) && CPV->getType()->isFirstClassType()) {
653 printType(Out, CPV->getType());
654 Out << ")/*UNDEF*/0)";
658 switch (CPV->getType()->getTypeID()) {
660 Out << (cast<ConstantBool>(CPV)->getValue() ? '1' : '0');
662 case Type::SByteTyID:
663 case Type::ShortTyID:
664 Out << cast<ConstantInt>(CPV)->getSExtValue();
667 if ((int)cast<ConstantInt>(CPV)->getSExtValue() == (int)0x80000000)
668 Out << "((int)0x80000000U)"; // Handle MININT specially to avoid warning
670 Out << cast<ConstantInt>(CPV)->getSExtValue();
674 if (cast<ConstantInt>(CPV)->isMinValue())
675 Out << "(/*INT64_MIN*/(-9223372036854775807LL)-1)";
677 Out << cast<ConstantInt>(CPV)->getSExtValue() << "ll";
680 case Type::UByteTyID:
681 case Type::UShortTyID:
682 Out << cast<ConstantInt>(CPV)->getZExtValue();
685 Out << cast<ConstantInt>(CPV)->getZExtValue() << 'u';
687 case Type::ULongTyID:
688 Out << cast<ConstantInt>(CPV)->getZExtValue() << "ull";
691 case Type::FloatTyID:
692 case Type::DoubleTyID: {
693 ConstantFP *FPC = cast<ConstantFP>(CPV);
694 std::map<const ConstantFP*, unsigned>::iterator I = FPConstantMap.find(FPC);
695 if (I != FPConstantMap.end()) {
696 // Because of FP precision problems we must load from a stack allocated
697 // value that holds the value in hex.
698 Out << "(*(" << (FPC->getType() == Type::FloatTy ? "float" : "double")
699 << "*)&FPConstant" << I->second << ')';
701 if (IsNAN(FPC->getValue())) {
704 // The prefix for a quiet NaN is 0x7FF8. For a signalling NaN,
706 const unsigned long QuietNaN = 0x7ff8UL;
707 //const unsigned long SignalNaN = 0x7ff4UL;
709 // We need to grab the first part of the FP #
712 uint64_t ll = DoubleToBits(FPC->getValue());
713 sprintf(Buffer, "0x%llx", static_cast<long long>(ll));
715 std::string Num(&Buffer[0], &Buffer[6]);
716 unsigned long Val = strtoul(Num.c_str(), 0, 16);
718 if (FPC->getType() == Type::FloatTy)
719 Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "F(\""
720 << Buffer << "\") /*nan*/ ";
722 Out << "LLVM_NAN" << (Val == QuietNaN ? "" : "S") << "(\""
723 << Buffer << "\") /*nan*/ ";
724 } else if (IsInf(FPC->getValue())) {
726 if (FPC->getValue() < 0) Out << '-';
727 Out << "LLVM_INF" << (FPC->getType() == Type::FloatTy ? "F" : "")
731 #if HAVE_PRINTF_A && ENABLE_CBE_PRINTF_A
732 // Print out the constant as a floating point number.
734 sprintf(Buffer, "%a", FPC->getValue());
737 Num = ftostr(FPC->getValue());
745 case Type::ArrayTyID:
746 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
747 const ArrayType *AT = cast<ArrayType>(CPV->getType());
749 if (AT->getNumElements()) {
751 Constant *CZ = Constant::getNullValue(AT->getElementType());
753 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
760 printConstantArray(cast<ConstantArray>(CPV));
764 case Type::PackedTyID:
765 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
766 const PackedType *AT = cast<PackedType>(CPV->getType());
768 if (AT->getNumElements()) {
770 Constant *CZ = Constant::getNullValue(AT->getElementType());
772 for (unsigned i = 1, e = AT->getNumElements(); i != e; ++i) {
779 printConstantPacked(cast<ConstantPacked>(CPV));
783 case Type::StructTyID:
784 if (isa<ConstantAggregateZero>(CPV) || isa<UndefValue>(CPV)) {
785 const StructType *ST = cast<StructType>(CPV->getType());
787 if (ST->getNumElements()) {
789 printConstant(Constant::getNullValue(ST->getElementType(0)));
790 for (unsigned i = 1, e = ST->getNumElements(); i != e; ++i) {
792 printConstant(Constant::getNullValue(ST->getElementType(i)));
798 if (CPV->getNumOperands()) {
800 printConstant(cast<Constant>(CPV->getOperand(0)));
801 for (unsigned i = 1, e = CPV->getNumOperands(); i != e; ++i) {
803 printConstant(cast<Constant>(CPV->getOperand(i)));
810 case Type::PointerTyID:
811 if (isa<ConstantPointerNull>(CPV)) {
813 printType(Out, CPV->getType());
814 Out << ")/*NULL*/0)";
816 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(CPV)) {
822 std::cerr << "Unknown constant type: " << *CPV << "\n";
827 // Some constant expressions need to be casted back to the original types
828 // because their operands were casted to the expected type. This function takes
829 // care of detecting that case and printing the cast for the ConstantExpr.
830 bool CWriter::printConstExprCast(const ConstantExpr* CE) {
831 bool NeedsExplicitCast = false;
832 const Type* Ty = CE->getOperand(0)->getType();
833 switch (CE->getOpcode()) {
834 case Instruction::LShr:
835 case Instruction::URem:
836 case Instruction::UDiv: NeedsExplicitCast = Ty->isSigned(); break;
837 case Instruction::AShr:
838 case Instruction::SRem:
839 case Instruction::SDiv: NeedsExplicitCast = Ty->isUnsigned(); break;
842 if (NeedsExplicitCast) {
847 return NeedsExplicitCast;
850 // Print a constant assuming that it is the operand for a given Opcode. The
851 // opcodes that care about sign need to cast their operands to the expected
852 // type before the operation proceeds. This function does the casting.
853 void CWriter::printConstantWithCast(Constant* CPV, unsigned Opcode) {
855 // Extract the operand's type, we'll need it.
856 const Type* OpTy = CPV->getType();
858 // Indicate whether to do the cast or not.
859 bool shouldCast = false;
861 // Based on the Opcode for which this Constant is being written, determine
862 // the new type to which the operand should be casted by setting the value
863 // of OpTy. If we change OpTy, also set shouldCast to true.
866 // for most instructions, it doesn't matter
868 case Instruction::LShr:
869 case Instruction::UDiv:
870 case Instruction::URem:
871 // For UDiv/URem get correct type
872 if (OpTy->isSigned()) {
873 OpTy = OpTy->getUnsignedVersion();
877 case Instruction::AShr:
878 case Instruction::SDiv:
879 case Instruction::SRem:
880 // For SDiv/SRem get correct type
881 if (OpTy->isUnsigned()) {
882 OpTy = OpTy->getSignedVersion();
888 // Write out the casted constnat if we should, otherwise just write the
892 printType(Out, OpTy);
901 void CWriter::writeOperandInternal(Value *Operand) {
902 if (Instruction *I = dyn_cast<Instruction>(Operand))
903 if (isInlinableInst(*I) && !isDirectAlloca(I)) {
904 // Should we inline this instruction to build a tree?
911 Constant* CPV = dyn_cast<Constant>(Operand);
912 if (CPV && !isa<GlobalValue>(CPV)) {
915 Out << Mang->getValueName(Operand);
919 void CWriter::writeOperand(Value *Operand) {
920 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
921 Out << "(&"; // Global variables are references as their addresses by llvm
923 writeOperandInternal(Operand);
925 if (isa<GlobalVariable>(Operand) || isDirectAlloca(Operand))
929 // Some instructions need to have their result value casted back to the
930 // original types because their operands were casted to the expected type.
931 // This function takes care of detecting that case and printing the cast
932 // for the Instruction.
933 bool CWriter::writeInstructionCast(const Instruction &I) {
934 bool NeedsExplicitCast = false;
935 const Type* Ty = I.getOperand(0)->getType();
936 switch (I.getOpcode()) {
937 case Instruction::LShr:
938 case Instruction::URem:
939 case Instruction::UDiv: NeedsExplicitCast = Ty->isSigned(); break;
940 case Instruction::AShr:
941 case Instruction::SRem:
942 case Instruction::SDiv: NeedsExplicitCast = Ty->isUnsigned(); break;
945 if (NeedsExplicitCast) {
950 return NeedsExplicitCast;
953 // Write the operand with a cast to another type based on the Opcode being used.
954 // This will be used in cases where an instruction has specific type
955 // requirements (usually signedness) for its operands.
956 void CWriter::writeOperandWithCast(Value* Operand, unsigned Opcode) {
958 // Extract the operand's type, we'll need it.
959 const Type* OpTy = Operand->getType();
961 // Indicate whether to do the cast or not.
962 bool shouldCast = false;
964 // Based on the Opcode for which this Operand is being written, determine
965 // the new type to which the operand should be casted by setting the value
966 // of OpTy. If we change OpTy, also set shouldCast to true.
969 // for most instructions, it doesn't matter
971 case Instruction::LShr:
972 case Instruction::UDiv:
973 case Instruction::URem:
974 // For UDiv to have unsigned operands
975 if (OpTy->isSigned()) {
976 OpTy = OpTy->getUnsignedVersion();
980 case Instruction::AShr:
981 case Instruction::SDiv:
982 case Instruction::SRem:
983 if (OpTy->isUnsigned()) {
984 OpTy = OpTy->getSignedVersion();
990 // Write out the casted operand if we should, otherwise just write the
994 printType(Out, OpTy);
996 writeOperand(Operand);
999 writeOperand(Operand);
1003 // generateCompilerSpecificCode - This is where we add conditional compilation
1004 // directives to cater to specific compilers as need be.
1006 static void generateCompilerSpecificCode(std::ostream& Out) {
1007 // Alloca is hard to get, and we don't want to include stdlib.h here.
1008 Out << "/* get a declaration for alloca */\n"
1009 << "#if defined(__CYGWIN__) || defined(__MINGW32__)\n"
1010 << "extern void *_alloca(unsigned long);\n"
1011 << "#define alloca(x) _alloca(x)\n"
1012 << "#elif defined(__APPLE__)\n"
1013 << "extern void *__builtin_alloca(unsigned long);\n"
1014 << "#define alloca(x) __builtin_alloca(x)\n"
1015 << "#elif defined(__sun__)\n"
1016 << "#if defined(__sparcv9)\n"
1017 << "extern void *__builtin_alloca(unsigned long);\n"
1019 << "extern void *__builtin_alloca(unsigned int);\n"
1021 << "#define alloca(x) __builtin_alloca(x)\n"
1022 << "#elif defined(__FreeBSD__) || defined(__OpenBSD__)\n"
1023 << "#define alloca(x) __builtin_alloca(x)\n"
1024 << "#elif !defined(_MSC_VER)\n"
1025 << "#include <alloca.h>\n"
1028 // We output GCC specific attributes to preserve 'linkonce'ness on globals.
1029 // If we aren't being compiled with GCC, just drop these attributes.
1030 Out << "#ifndef __GNUC__ /* Can only support \"linkonce\" vars with GCC */\n"
1031 << "#define __attribute__(X)\n"
1035 // At some point, we should support "external weak" vs. "weak" linkages.
1036 // On Mac OS X, "external weak" is spelled "__attribute__((weak_import))".
1037 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
1038 << "#define __EXTERNAL_WEAK__ __attribute__((weak_import))\n"
1039 << "#elif defined(__GNUC__)\n"
1040 << "#define __EXTERNAL_WEAK__ __attribute__((weak))\n"
1042 << "#define __EXTERNAL_WEAK__\n"
1046 // For now, turn off the weak linkage attribute on Mac OS X. (See above.)
1047 Out << "#if defined(__GNUC__) && defined(__APPLE_CC__)\n"
1048 << "#define __ATTRIBUTE_WEAK__\n"
1049 << "#elif defined(__GNUC__)\n"
1050 << "#define __ATTRIBUTE_WEAK__ __attribute__((weak))\n"
1052 << "#define __ATTRIBUTE_WEAK__\n"
1055 // Define NaN and Inf as GCC builtins if using GCC, as 0 otherwise
1056 // From the GCC documentation:
1058 // double __builtin_nan (const char *str)
1060 // This is an implementation of the ISO C99 function nan.
1062 // Since ISO C99 defines this function in terms of strtod, which we do
1063 // not implement, a description of the parsing is in order. The string is
1064 // parsed as by strtol; that is, the base is recognized by leading 0 or
1065 // 0x prefixes. The number parsed is placed in the significand such that
1066 // the least significant bit of the number is at the least significant
1067 // bit of the significand. The number is truncated to fit the significand
1068 // field provided. The significand is forced to be a quiet NaN.
1070 // This function, if given a string literal, is evaluated early enough
1071 // that it is considered a compile-time constant.
1073 // float __builtin_nanf (const char *str)
1075 // Similar to __builtin_nan, except the return type is float.
1077 // double __builtin_inf (void)
1079 // Similar to __builtin_huge_val, except a warning is generated if the
1080 // target floating-point format does not support infinities. This
1081 // function is suitable for implementing the ISO C99 macro INFINITY.
1083 // float __builtin_inff (void)
1085 // Similar to __builtin_inf, except the return type is float.
1086 Out << "#ifdef __GNUC__\n"
1087 << "#define LLVM_NAN(NanStr) __builtin_nan(NanStr) /* Double */\n"
1088 << "#define LLVM_NANF(NanStr) __builtin_nanf(NanStr) /* Float */\n"
1089 << "#define LLVM_NANS(NanStr) __builtin_nans(NanStr) /* Double */\n"
1090 << "#define LLVM_NANSF(NanStr) __builtin_nansf(NanStr) /* Float */\n"
1091 << "#define LLVM_INF __builtin_inf() /* Double */\n"
1092 << "#define LLVM_INFF __builtin_inff() /* Float */\n"
1093 << "#define LLVM_PREFETCH(addr,rw,locality) "
1094 "__builtin_prefetch(addr,rw,locality)\n"
1095 << "#define __ATTRIBUTE_CTOR__ __attribute__((constructor))\n"
1096 << "#define __ATTRIBUTE_DTOR__ __attribute__((destructor))\n"
1097 << "#define LLVM_ASM __asm__\n"
1099 << "#define LLVM_NAN(NanStr) ((double)0.0) /* Double */\n"
1100 << "#define LLVM_NANF(NanStr) 0.0F /* Float */\n"
1101 << "#define LLVM_NANS(NanStr) ((double)0.0) /* Double */\n"
1102 << "#define LLVM_NANSF(NanStr) 0.0F /* Float */\n"
1103 << "#define LLVM_INF ((double)0.0) /* Double */\n"
1104 << "#define LLVM_INFF 0.0F /* Float */\n"
1105 << "#define LLVM_PREFETCH(addr,rw,locality) /* PREFETCH */\n"
1106 << "#define __ATTRIBUTE_CTOR__\n"
1107 << "#define __ATTRIBUTE_DTOR__\n"
1108 << "#define LLVM_ASM(X)\n"
1111 // Output target-specific code that should be inserted into main.
1112 Out << "#define CODE_FOR_MAIN() /* Any target-specific code for main()*/\n";
1113 // On X86, set the FP control word to 64-bits of precision instead of 80 bits.
1114 Out << "#if defined(__GNUC__) && !defined(__llvm__)\n"
1115 << "#if defined(i386) || defined(__i386__) || defined(__i386) || "
1116 << "defined(__x86_64__)\n"
1117 << "#undef CODE_FOR_MAIN\n"
1118 << "#define CODE_FOR_MAIN() \\\n"
1119 << " {short F;__asm__ (\"fnstcw %0\" : \"=m\" (*&F)); \\\n"
1120 << " F=(F&~0x300)|0x200;__asm__(\"fldcw %0\"::\"m\"(*&F));}\n"
1121 << "#endif\n#endif\n";
1125 /// FindStaticTors - Given a static ctor/dtor list, unpack its contents into
1126 /// the StaticTors set.
1127 static void FindStaticTors(GlobalVariable *GV, std::set<Function*> &StaticTors){
1128 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1129 if (!InitList) return;
1131 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
1132 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
1133 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
1135 if (CS->getOperand(1)->isNullValue())
1136 return; // Found a null terminator, exit printing.
1137 Constant *FP = CS->getOperand(1);
1138 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
1139 if (CE->getOpcode() == Instruction::Cast)
1140 FP = CE->getOperand(0);
1141 if (Function *F = dyn_cast<Function>(FP))
1142 StaticTors.insert(F);
1146 enum SpecialGlobalClass {
1148 GlobalCtors, GlobalDtors,
1152 /// getGlobalVariableClass - If this is a global that is specially recognized
1153 /// by LLVM, return a code that indicates how we should handle it.
1154 static SpecialGlobalClass getGlobalVariableClass(const GlobalVariable *GV) {
1155 // If this is a global ctors/dtors list, handle it now.
1156 if (GV->hasAppendingLinkage() && GV->use_empty()) {
1157 if (GV->getName() == "llvm.global_ctors")
1159 else if (GV->getName() == "llvm.global_dtors")
1163 // Otherwise, it it is other metadata, don't print it. This catches things
1164 // like debug information.
1165 if (GV->getSection() == "llvm.metadata")
1172 bool CWriter::doInitialization(Module &M) {
1176 IL.AddPrototypes(M);
1178 // Ensure that all structure types have names...
1179 Mang = new Mangler(M);
1180 Mang->markCharUnacceptable('.');
1182 // Keep track of which functions are static ctors/dtors so they can have
1183 // an attribute added to their prototypes.
1184 std::set<Function*> StaticCtors, StaticDtors;
1185 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1187 switch (getGlobalVariableClass(I)) {
1190 FindStaticTors(I, StaticCtors);
1193 FindStaticTors(I, StaticDtors);
1198 // get declaration for alloca
1199 Out << "/* Provide Declarations */\n";
1200 Out << "#include <stdarg.h>\n"; // Varargs support
1201 Out << "#include <setjmp.h>\n"; // Unwind support
1202 generateCompilerSpecificCode(Out);
1204 // Provide a definition for `bool' if not compiling with a C++ compiler.
1206 << "#ifndef __cplusplus\ntypedef unsigned char bool;\n#endif\n"
1208 << "\n\n/* Support for floating point constants */\n"
1209 << "typedef unsigned long long ConstantDoubleTy;\n"
1210 << "typedef unsigned int ConstantFloatTy;\n"
1212 << "\n\n/* Global Declarations */\n";
1214 // First output all the declarations for the program, because C requires
1215 // Functions & globals to be declared before they are used.
1218 // Loop over the symbol table, emitting all named constants...
1219 printModuleTypes(M.getSymbolTable());
1221 // Global variable declarations...
1222 if (!M.global_empty()) {
1223 Out << "\n/* External Global Variable Declarations */\n";
1224 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1226 if (I->hasExternalLinkage()) {
1228 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
1230 } else if (I->hasDLLImportLinkage()) {
1231 Out << "__declspec(dllimport) ";
1232 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
1238 // Function declarations
1239 Out << "\n/* Function Declarations */\n";
1240 Out << "double fmod(double, double);\n"; // Support for FP rem
1241 Out << "float fmodf(float, float);\n";
1243 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
1244 // Don't print declarations for intrinsic functions.
1245 if (!I->getIntrinsicID() && I->getName() != "setjmp" &&
1246 I->getName() != "longjmp" && I->getName() != "_setjmp") {
1247 printFunctionSignature(I, true);
1248 if (I->hasWeakLinkage() || I->hasLinkOnceLinkage())
1249 Out << " __ATTRIBUTE_WEAK__";
1250 if (StaticCtors.count(I))
1251 Out << " __ATTRIBUTE_CTOR__";
1252 if (StaticDtors.count(I))
1253 Out << " __ATTRIBUTE_DTOR__";
1255 if (I->hasName() && I->getName()[0] == 1)
1256 Out << " LLVM_ASM(\"" << I->getName().c_str()+1 << "\")";
1262 // Output the global variable declarations
1263 if (!M.global_empty()) {
1264 Out << "\n\n/* Global Variable Declarations */\n";
1265 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1267 if (!I->isExternal()) {
1268 // Ignore special globals, such as debug info.
1269 if (getGlobalVariableClass(I))
1272 if (I->hasInternalLinkage())
1276 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
1278 if (I->hasLinkOnceLinkage())
1279 Out << " __attribute__((common))";
1280 else if (I->hasWeakLinkage())
1281 Out << " __ATTRIBUTE_WEAK__";
1286 // Output the global variable definitions and contents...
1287 if (!M.global_empty()) {
1288 Out << "\n\n/* Global Variable Definitions and Initialization */\n";
1289 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
1291 if (!I->isExternal()) {
1292 // Ignore special globals, such as debug info.
1293 if (getGlobalVariableClass(I))
1296 if (I->hasInternalLinkage())
1298 else if (I->hasDLLImportLinkage())
1299 Out << "__declspec(dllimport) ";
1300 else if (I->hasDLLExportLinkage())
1301 Out << "__declspec(dllexport) ";
1303 printType(Out, I->getType()->getElementType(), Mang->getValueName(I));
1304 if (I->hasLinkOnceLinkage())
1305 Out << " __attribute__((common))";
1306 else if (I->hasWeakLinkage())
1307 Out << " __ATTRIBUTE_WEAK__";
1309 // If the initializer is not null, emit the initializer. If it is null,
1310 // we try to avoid emitting large amounts of zeros. The problem with
1311 // this, however, occurs when the variable has weak linkage. In this
1312 // case, the assembler will complain about the variable being both weak
1313 // and common, so we disable this optimization.
1314 if (!I->getInitializer()->isNullValue()) {
1316 writeOperand(I->getInitializer());
1317 } else if (I->hasWeakLinkage()) {
1318 // We have to specify an initializer, but it doesn't have to be
1319 // complete. If the value is an aggregate, print out { 0 }, and let
1320 // the compiler figure out the rest of the zeros.
1322 if (isa<StructType>(I->getInitializer()->getType()) ||
1323 isa<ArrayType>(I->getInitializer()->getType()) ||
1324 isa<PackedType>(I->getInitializer()->getType())) {
1327 // Just print it out normally.
1328 writeOperand(I->getInitializer());
1336 Out << "\n\n/* Function Bodies */\n";
1341 /// Output all floating point constants that cannot be printed accurately...
1342 void CWriter::printFloatingPointConstants(Function &F) {
1343 // Scan the module for floating point constants. If any FP constant is used
1344 // in the function, we want to redirect it here so that we do not depend on
1345 // the precision of the printed form, unless the printed form preserves
1348 static unsigned FPCounter = 0;
1349 for (constant_iterator I = constant_begin(&F), E = constant_end(&F);
1351 if (const ConstantFP *FPC = dyn_cast<ConstantFP>(*I))
1352 if (!isFPCSafeToPrint(FPC) && // Do not put in FPConstantMap if safe.
1353 !FPConstantMap.count(FPC)) {
1354 double Val = FPC->getValue();
1356 FPConstantMap[FPC] = FPCounter; // Number the FP constants
1358 if (FPC->getType() == Type::DoubleTy) {
1359 Out << "static const ConstantDoubleTy FPConstant" << FPCounter++
1360 << " = 0x" << std::hex << DoubleToBits(Val) << std::dec
1361 << "ULL; /* " << Val << " */\n";
1362 } else if (FPC->getType() == Type::FloatTy) {
1363 Out << "static const ConstantFloatTy FPConstant" << FPCounter++
1364 << " = 0x" << std::hex << FloatToBits(Val) << std::dec
1365 << "U; /* " << Val << " */\n";
1367 assert(0 && "Unknown float type!");
1374 /// printSymbolTable - Run through symbol table looking for type names. If a
1375 /// type name is found, emit its declaration...
1377 void CWriter::printModuleTypes(const SymbolTable &ST) {
1378 // We are only interested in the type plane of the symbol table.
1379 SymbolTable::type_const_iterator I = ST.type_begin();
1380 SymbolTable::type_const_iterator End = ST.type_end();
1382 // If there are no type names, exit early.
1383 if (I == End) return;
1385 // Print out forward declarations for structure types before anything else!
1386 Out << "/* Structure forward decls */\n";
1387 for (; I != End; ++I)
1388 if (const Type *STy = dyn_cast<StructType>(I->second)) {
1389 std::string Name = "struct l_" + Mang->makeNameProper(I->first);
1390 Out << Name << ";\n";
1391 TypeNames.insert(std::make_pair(STy, Name));
1396 // Now we can print out typedefs...
1397 Out << "/* Typedefs */\n";
1398 for (I = ST.type_begin(); I != End; ++I) {
1399 const Type *Ty = cast<Type>(I->second);
1400 std::string Name = "l_" + Mang->makeNameProper(I->first);
1402 printType(Out, Ty, Name);
1408 // Keep track of which structures have been printed so far...
1409 std::set<const StructType *> StructPrinted;
1411 // Loop over all structures then push them into the stack so they are
1412 // printed in the correct order.
1414 Out << "/* Structure contents */\n";
1415 for (I = ST.type_begin(); I != End; ++I)
1416 if (const StructType *STy = dyn_cast<StructType>(I->second))
1417 // Only print out used types!
1418 printContainedStructs(STy, StructPrinted);
1421 // Push the struct onto the stack and recursively push all structs
1422 // this one depends on.
1424 // TODO: Make this work properly with packed types
1426 void CWriter::printContainedStructs(const Type *Ty,
1427 std::set<const StructType*> &StructPrinted){
1428 // Don't walk through pointers.
1429 if (isa<PointerType>(Ty) || Ty->isPrimitiveType()) return;
1431 // Print all contained types first.
1432 for (Type::subtype_iterator I = Ty->subtype_begin(),
1433 E = Ty->subtype_end(); I != E; ++I)
1434 printContainedStructs(*I, StructPrinted);
1436 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
1437 // Check to see if we have already printed this struct.
1438 if (StructPrinted.insert(STy).second) {
1439 // Print structure type out.
1440 std::string Name = TypeNames[STy];
1441 printType(Out, STy, Name, true);
1447 void CWriter::printFunctionSignature(const Function *F, bool Prototype) {
1448 /// isCStructReturn - Should this function actually return a struct by-value?
1449 bool isCStructReturn = F->getCallingConv() == CallingConv::CSRet;
1451 if (F->hasInternalLinkage()) Out << "static ";
1452 if (F->hasDLLImportLinkage()) Out << "__declspec(dllimport) ";
1453 if (F->hasDLLExportLinkage()) Out << "__declspec(dllexport) ";
1454 switch (F->getCallingConv()) {
1455 case CallingConv::X86_StdCall:
1456 Out << "__stdcall ";
1458 case CallingConv::X86_FastCall:
1459 Out << "__fastcall ";
1463 // Loop over the arguments, printing them...
1464 const FunctionType *FT = cast<FunctionType>(F->getFunctionType());
1466 std::stringstream FunctionInnards;
1468 // Print out the name...
1469 FunctionInnards << Mang->getValueName(F) << '(';
1471 bool PrintedArg = false;
1472 if (!F->isExternal()) {
1473 if (!F->arg_empty()) {
1474 Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1476 // If this is a struct-return function, don't print the hidden
1477 // struct-return argument.
1478 if (isCStructReturn) {
1479 assert(I != E && "Invalid struct return function!");
1483 std::string ArgName;
1484 for (; I != E; ++I) {
1485 if (PrintedArg) FunctionInnards << ", ";
1486 if (I->hasName() || !Prototype)
1487 ArgName = Mang->getValueName(I);
1490 printType(FunctionInnards, I->getType(), ArgName);
1495 // Loop over the arguments, printing them.
1496 FunctionType::param_iterator I = FT->param_begin(), E = FT->param_end();
1498 // If this is a struct-return function, don't print the hidden
1499 // struct-return argument.
1500 if (isCStructReturn) {
1501 assert(I != E && "Invalid struct return function!");
1505 for (; I != E; ++I) {
1506 if (PrintedArg) FunctionInnards << ", ";
1507 printType(FunctionInnards, *I);
1512 // Finish printing arguments... if this is a vararg function, print the ...,
1513 // unless there are no known types, in which case, we just emit ().
1515 if (FT->isVarArg() && PrintedArg) {
1516 if (PrintedArg) FunctionInnards << ", ";
1517 FunctionInnards << "..."; // Output varargs portion of signature!
1518 } else if (!FT->isVarArg() && !PrintedArg) {
1519 FunctionInnards << "void"; // ret() -> ret(void) in C.
1521 FunctionInnards << ')';
1523 // Get the return tpe for the function.
1525 if (!isCStructReturn)
1526 RetTy = F->getReturnType();
1528 // If this is a struct-return function, print the struct-return type.
1529 RetTy = cast<PointerType>(FT->getParamType(0))->getElementType();
1532 // Print out the return type and the signature built above.
1533 printType(Out, RetTy, FunctionInnards.str());
1536 void CWriter::printFunction(Function &F) {
1537 printFunctionSignature(&F, false);
1540 // If this is a struct return function, handle the result with magic.
1541 if (F.getCallingConv() == CallingConv::CSRet) {
1542 const Type *StructTy =
1543 cast<PointerType>(F.arg_begin()->getType())->getElementType();
1545 printType(Out, StructTy, "StructReturn");
1546 Out << "; /* Struct return temporary */\n";
1549 printType(Out, F.arg_begin()->getType(), Mang->getValueName(F.arg_begin()));
1550 Out << " = &StructReturn;\n";
1553 bool PrintedVar = false;
1555 // print local variable information for the function
1556 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ++I)
1557 if (const AllocaInst *AI = isDirectAlloca(&*I)) {
1559 printType(Out, AI->getAllocatedType(), Mang->getValueName(AI));
1560 Out << "; /* Address-exposed local */\n";
1562 } else if (I->getType() != Type::VoidTy && !isInlinableInst(*I)) {
1564 printType(Out, I->getType(), Mang->getValueName(&*I));
1567 if (isa<PHINode>(*I)) { // Print out PHI node temporaries as well...
1569 printType(Out, I->getType(),
1570 Mang->getValueName(&*I)+"__PHI_TEMPORARY");
1579 if (F.hasExternalLinkage() && F.getName() == "main")
1580 Out << " CODE_FOR_MAIN();\n";
1582 // print the basic blocks
1583 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
1584 if (Loop *L = LI->getLoopFor(BB)) {
1585 if (L->getHeader() == BB && L->getParentLoop() == 0)
1588 printBasicBlock(BB);
1595 void CWriter::printLoop(Loop *L) {
1596 Out << " do { /* Syntactic loop '" << L->getHeader()->getName()
1597 << "' to make GCC happy */\n";
1598 for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) {
1599 BasicBlock *BB = L->getBlocks()[i];
1600 Loop *BBLoop = LI->getLoopFor(BB);
1602 printBasicBlock(BB);
1603 else if (BB == BBLoop->getHeader() && BBLoop->getParentLoop() == L)
1606 Out << " } while (1); /* end of syntactic loop '"
1607 << L->getHeader()->getName() << "' */\n";
1610 void CWriter::printBasicBlock(BasicBlock *BB) {
1612 // Don't print the label for the basic block if there are no uses, or if
1613 // the only terminator use is the predecessor basic block's terminator.
1614 // We have to scan the use list because PHI nodes use basic blocks too but
1615 // do not require a label to be generated.
1617 bool NeedsLabel = false;
1618 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
1619 if (isGotoCodeNecessary(*PI, BB)) {
1624 if (NeedsLabel) Out << Mang->getValueName(BB) << ":\n";
1626 // Output all of the instructions in the basic block...
1627 for (BasicBlock::iterator II = BB->begin(), E = --BB->end(); II != E;
1629 if (!isInlinableInst(*II) && !isDirectAlloca(II)) {
1630 if (II->getType() != Type::VoidTy)
1639 // Don't emit prefix or suffix for the terminator...
1640 visit(*BB->getTerminator());
1644 // Specific Instruction type classes... note that all of the casts are
1645 // necessary because we use the instruction classes as opaque types...
1647 void CWriter::visitReturnInst(ReturnInst &I) {
1648 // If this is a struct return function, return the temporary struct.
1649 if (I.getParent()->getParent()->getCallingConv() == CallingConv::CSRet) {
1650 Out << " return StructReturn;\n";
1654 // Don't output a void return if this is the last basic block in the function
1655 if (I.getNumOperands() == 0 &&
1656 &*--I.getParent()->getParent()->end() == I.getParent() &&
1657 !I.getParent()->size() == 1) {
1662 if (I.getNumOperands()) {
1664 writeOperand(I.getOperand(0));
1669 void CWriter::visitSwitchInst(SwitchInst &SI) {
1672 writeOperand(SI.getOperand(0));
1673 Out << ") {\n default:\n";
1674 printPHICopiesForSuccessor (SI.getParent(), SI.getDefaultDest(), 2);
1675 printBranchToBlock(SI.getParent(), SI.getDefaultDest(), 2);
1677 for (unsigned i = 2, e = SI.getNumOperands(); i != e; i += 2) {
1679 writeOperand(SI.getOperand(i));
1681 BasicBlock *Succ = cast<BasicBlock>(SI.getOperand(i+1));
1682 printPHICopiesForSuccessor (SI.getParent(), Succ, 2);
1683 printBranchToBlock(SI.getParent(), Succ, 2);
1684 if (Function::iterator(Succ) == next(Function::iterator(SI.getParent())))
1690 void CWriter::visitUnreachableInst(UnreachableInst &I) {
1691 Out << " /*UNREACHABLE*/;\n";
1694 bool CWriter::isGotoCodeNecessary(BasicBlock *From, BasicBlock *To) {
1695 /// FIXME: This should be reenabled, but loop reordering safe!!
1698 if (next(Function::iterator(From)) != Function::iterator(To))
1699 return true; // Not the direct successor, we need a goto.
1701 //isa<SwitchInst>(From->getTerminator())
1703 if (LI->getLoopFor(From) != LI->getLoopFor(To))
1708 void CWriter::printPHICopiesForSuccessor (BasicBlock *CurBlock,
1709 BasicBlock *Successor,
1711 for (BasicBlock::iterator I = Successor->begin(); isa<PHINode>(I); ++I) {
1712 PHINode *PN = cast<PHINode>(I);
1713 // Now we have to do the printing.
1714 Value *IV = PN->getIncomingValueForBlock(CurBlock);
1715 if (!isa<UndefValue>(IV)) {
1716 Out << std::string(Indent, ' ');
1717 Out << " " << Mang->getValueName(I) << "__PHI_TEMPORARY = ";
1719 Out << "; /* for PHI node */\n";
1724 void CWriter::printBranchToBlock(BasicBlock *CurBB, BasicBlock *Succ,
1726 if (isGotoCodeNecessary(CurBB, Succ)) {
1727 Out << std::string(Indent, ' ') << " goto ";
1733 // Branch instruction printing - Avoid printing out a branch to a basic block
1734 // that immediately succeeds the current one.
1736 void CWriter::visitBranchInst(BranchInst &I) {
1738 if (I.isConditional()) {
1739 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(0))) {
1741 writeOperand(I.getCondition());
1744 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 2);
1745 printBranchToBlock(I.getParent(), I.getSuccessor(0), 2);
1747 if (isGotoCodeNecessary(I.getParent(), I.getSuccessor(1))) {
1748 Out << " } else {\n";
1749 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
1750 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
1753 // First goto not necessary, assume second one is...
1755 writeOperand(I.getCondition());
1758 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(1), 2);
1759 printBranchToBlock(I.getParent(), I.getSuccessor(1), 2);
1764 printPHICopiesForSuccessor (I.getParent(), I.getSuccessor(0), 0);
1765 printBranchToBlock(I.getParent(), I.getSuccessor(0), 0);
1770 // PHI nodes get copied into temporary values at the end of predecessor basic
1771 // blocks. We now need to copy these temporary values into the REAL value for
1773 void CWriter::visitPHINode(PHINode &I) {
1775 Out << "__PHI_TEMPORARY";
1779 void CWriter::visitBinaryOperator(Instruction &I) {
1780 // binary instructions, shift instructions, setCond instructions.
1781 assert(!isa<PointerType>(I.getType()));
1783 // We must cast the results of binary operations which might be promoted.
1784 bool needsCast = false;
1785 if ((I.getType() == Type::UByteTy) || (I.getType() == Type::SByteTy)
1786 || (I.getType() == Type::UShortTy) || (I.getType() == Type::ShortTy)
1787 || (I.getType() == Type::FloatTy)) {
1790 printType(Out, I.getType());
1794 // If this is a negation operation, print it out as such. For FP, we don't
1795 // want to print "-0.0 - X".
1796 if (BinaryOperator::isNeg(&I)) {
1798 writeOperand(BinaryOperator::getNegArgument(cast<BinaryOperator>(&I)));
1800 } else if (I.getOpcode() == Instruction::FRem) {
1801 // Output a call to fmod/fmodf instead of emitting a%b
1802 if (I.getType() == Type::FloatTy)
1806 writeOperand(I.getOperand(0));
1808 writeOperand(I.getOperand(1));
1812 // Write out the cast of the instruction's value back to the proper type
1814 bool NeedsClosingParens = writeInstructionCast(I);
1816 // Certain instructions require the operand to be forced to a specific type
1817 // so we use writeOperandWithCast here instead of writeOperand. Similarly
1818 // below for operand 1
1819 writeOperandWithCast(I.getOperand(0), I.getOpcode());
1821 switch (I.getOpcode()) {
1822 case Instruction::Add: Out << " + "; break;
1823 case Instruction::Sub: Out << " - "; break;
1824 case Instruction::Mul: Out << '*'; break;
1825 case Instruction::URem:
1826 case Instruction::SRem:
1827 case Instruction::FRem: Out << '%'; break;
1828 case Instruction::UDiv:
1829 case Instruction::SDiv:
1830 case Instruction::FDiv: Out << '/'; break;
1831 case Instruction::And: Out << " & "; break;
1832 case Instruction::Or: Out << " | "; break;
1833 case Instruction::Xor: Out << " ^ "; break;
1834 case Instruction::SetEQ: Out << " == "; break;
1835 case Instruction::SetNE: Out << " != "; break;
1836 case Instruction::SetLE: Out << " <= "; break;
1837 case Instruction::SetGE: Out << " >= "; break;
1838 case Instruction::SetLT: Out << " < "; break;
1839 case Instruction::SetGT: Out << " > "; break;
1840 case Instruction::Shl : Out << " << "; break;
1841 case Instruction::LShr:
1842 case Instruction::AShr: Out << " >> "; break;
1843 default: std::cerr << "Invalid operator type!" << I; abort();
1846 writeOperandWithCast(I.getOperand(1), I.getOpcode());
1847 if (NeedsClosingParens)
1856 void CWriter::visitCastInst(CastInst &I) {
1857 if (I.getType() == Type::BoolTy) {
1859 writeOperand(I.getOperand(0));
1864 printType(Out, I.getType());
1866 if (isa<PointerType>(I.getType())&&I.getOperand(0)->getType()->isIntegral() ||
1867 isa<PointerType>(I.getOperand(0)->getType())&&I.getType()->isIntegral()) {
1868 // Avoid "cast to pointer from integer of different size" warnings
1872 writeOperand(I.getOperand(0));
1875 void CWriter::visitSelectInst(SelectInst &I) {
1877 writeOperand(I.getCondition());
1879 writeOperand(I.getTrueValue());
1881 writeOperand(I.getFalseValue());
1886 void CWriter::lowerIntrinsics(Function &F) {
1887 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
1888 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
1889 if (CallInst *CI = dyn_cast<CallInst>(I++))
1890 if (Function *F = CI->getCalledFunction())
1891 switch (F->getIntrinsicID()) {
1892 case Intrinsic::not_intrinsic:
1893 case Intrinsic::vastart:
1894 case Intrinsic::vacopy:
1895 case Intrinsic::vaend:
1896 case Intrinsic::returnaddress:
1897 case Intrinsic::frameaddress:
1898 case Intrinsic::setjmp:
1899 case Intrinsic::longjmp:
1900 case Intrinsic::prefetch:
1901 case Intrinsic::dbg_stoppoint:
1902 case Intrinsic::powi_f32:
1903 case Intrinsic::powi_f64:
1904 // We directly implement these intrinsics
1907 // If this is an intrinsic that directly corresponds to a GCC
1908 // builtin, we handle it.
1909 const char *BuiltinName = "";
1910 #define GET_GCC_BUILTIN_NAME
1911 #include "llvm/Intrinsics.gen"
1912 #undef GET_GCC_BUILTIN_NAME
1913 // If we handle it, don't lower it.
1914 if (BuiltinName[0]) break;
1916 // All other intrinsic calls we must lower.
1917 Instruction *Before = 0;
1918 if (CI != &BB->front())
1919 Before = prior(BasicBlock::iterator(CI));
1921 IL.LowerIntrinsicCall(CI);
1922 if (Before) { // Move iterator to instruction after call
1933 void CWriter::visitCallInst(CallInst &I) {
1934 bool WroteCallee = false;
1936 // Handle intrinsic function calls first...
1937 if (Function *F = I.getCalledFunction())
1938 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) {
1941 // If this is an intrinsic that directly corresponds to a GCC
1942 // builtin, we emit it here.
1943 const char *BuiltinName = "";
1944 #define GET_GCC_BUILTIN_NAME
1945 #include "llvm/Intrinsics.gen"
1946 #undef GET_GCC_BUILTIN_NAME
1947 assert(BuiltinName[0] && "Unknown LLVM intrinsic!");
1953 case Intrinsic::vastart:
1956 Out << "va_start(*(va_list*)";
1957 writeOperand(I.getOperand(1));
1959 // Output the last argument to the enclosing function...
1960 if (I.getParent()->getParent()->arg_empty()) {
1961 std::cerr << "The C backend does not currently support zero "
1962 << "argument varargs functions, such as '"
1963 << I.getParent()->getParent()->getName() << "'!\n";
1966 writeOperand(--I.getParent()->getParent()->arg_end());
1969 case Intrinsic::vaend:
1970 if (!isa<ConstantPointerNull>(I.getOperand(1))) {
1971 Out << "0; va_end(*(va_list*)";
1972 writeOperand(I.getOperand(1));
1975 Out << "va_end(*(va_list*)0)";
1978 case Intrinsic::vacopy:
1980 Out << "va_copy(*(va_list*)";
1981 writeOperand(I.getOperand(1));
1982 Out << ", *(va_list*)";
1983 writeOperand(I.getOperand(2));
1986 case Intrinsic::returnaddress:
1987 Out << "__builtin_return_address(";
1988 writeOperand(I.getOperand(1));
1991 case Intrinsic::frameaddress:
1992 Out << "__builtin_frame_address(";
1993 writeOperand(I.getOperand(1));
1996 case Intrinsic::powi_f32:
1997 case Intrinsic::powi_f64:
1998 Out << "__builtin_powi(";
1999 writeOperand(I.getOperand(1));
2001 writeOperand(I.getOperand(2));
2004 case Intrinsic::setjmp:
2005 #if defined(HAVE__SETJMP) && defined(HAVE__LONGJMP)
2006 Out << "_"; // Use _setjmp on systems that support it!
2008 Out << "setjmp(*(jmp_buf*)";
2009 writeOperand(I.getOperand(1));
2012 case Intrinsic::longjmp:
2013 #if defined(HAVE__SETJMP) && defined(HAVE__LONGJMP)
2014 Out << "_"; // Use _longjmp on systems that support it!
2016 Out << "longjmp(*(jmp_buf*)";
2017 writeOperand(I.getOperand(1));
2019 writeOperand(I.getOperand(2));
2022 case Intrinsic::prefetch:
2023 Out << "LLVM_PREFETCH((const void *)";
2024 writeOperand(I.getOperand(1));
2026 writeOperand(I.getOperand(2));
2028 writeOperand(I.getOperand(3));
2031 case Intrinsic::dbg_stoppoint: {
2032 // If we use writeOperand directly we get a "u" suffix which is rejected
2034 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
2038 << " \"" << SPI.getDirectory()
2039 << SPI.getFileName() << "\"\n";
2045 Value *Callee = I.getCalledValue();
2047 // If this is a call to a struct-return function, assign to the first
2048 // parameter instead of passing it to the call.
2049 bool isStructRet = I.getCallingConv() == CallingConv::CSRet;
2052 writeOperand(I.getOperand(1));
2056 if (I.isTailCall()) Out << " /*tail*/ ";
2058 const PointerType *PTy = cast<PointerType>(Callee->getType());
2059 const FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
2062 // If this is an indirect call to a struct return function, we need to cast
2064 bool NeedsCast = isStructRet && !isa<Function>(Callee);
2066 // GCC is a real PITA. It does not permit codegening casts of functions to
2067 // function pointers if they are in a call (it generates a trap instruction
2068 // instead!). We work around this by inserting a cast to void* in between
2069 // the function and the function pointer cast. Unfortunately, we can't just
2070 // form the constant expression here, because the folder will immediately
2073 // Note finally, that this is completely unsafe. ANSI C does not guarantee
2074 // that void* and function pointers have the same size. :( To deal with this
2075 // in the common case, we handle casts where the number of arguments passed
2078 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Callee))
2079 if (CE->getOpcode() == Instruction::Cast)
2080 if (Function *RF = dyn_cast<Function>(CE->getOperand(0))) {
2086 // Ok, just cast the pointer type.
2089 printType(Out, I.getCalledValue()->getType());
2091 printStructReturnPointerFunctionType(Out,
2092 cast<PointerType>(I.getCalledValue()->getType()));
2095 writeOperand(Callee);
2096 if (NeedsCast) Out << ')';
2101 unsigned NumDeclaredParams = FTy->getNumParams();
2103 CallSite::arg_iterator AI = I.op_begin()+1, AE = I.op_end();
2105 if (isStructRet) { // Skip struct return argument.
2110 bool PrintedArg = false;
2111 for (; AI != AE; ++AI, ++ArgNo) {
2112 if (PrintedArg) Out << ", ";
2113 if (ArgNo < NumDeclaredParams &&
2114 (*AI)->getType() != FTy->getParamType(ArgNo)) {
2116 printType(Out, FTy->getParamType(ArgNo));
2125 void CWriter::visitMallocInst(MallocInst &I) {
2126 assert(0 && "lowerallocations pass didn't work!");
2129 void CWriter::visitAllocaInst(AllocaInst &I) {
2131 printType(Out, I.getType());
2132 Out << ") alloca(sizeof(";
2133 printType(Out, I.getType()->getElementType());
2135 if (I.isArrayAllocation()) {
2137 writeOperand(I.getOperand(0));
2142 void CWriter::visitFreeInst(FreeInst &I) {
2143 assert(0 && "lowerallocations pass didn't work!");
2146 void CWriter::printIndexingExpression(Value *Ptr, gep_type_iterator I,
2147 gep_type_iterator E) {
2148 bool HasImplicitAddress = false;
2149 // If accessing a global value with no indexing, avoid *(&GV) syndrome
2150 if (isa<GlobalValue>(Ptr)) {
2151 HasImplicitAddress = true;
2152 } else if (isDirectAlloca(Ptr)) {
2153 HasImplicitAddress = true;
2157 if (!HasImplicitAddress)
2158 Out << '*'; // Implicit zero first argument: '*x' is equivalent to 'x[0]'
2160 writeOperandInternal(Ptr);
2164 const Constant *CI = dyn_cast<Constant>(I.getOperand());
2165 if (HasImplicitAddress && (!CI || !CI->isNullValue()))
2168 writeOperandInternal(Ptr);
2170 if (HasImplicitAddress && (!CI || !CI->isNullValue())) {
2172 HasImplicitAddress = false; // HIA is only true if we haven't addressed yet
2175 assert(!HasImplicitAddress || (CI && CI->isNullValue()) &&
2176 "Can only have implicit address with direct accessing");
2178 if (HasImplicitAddress) {
2180 } else if (CI && CI->isNullValue()) {
2181 gep_type_iterator TmpI = I; ++TmpI;
2183 // Print out the -> operator if possible...
2184 if (TmpI != E && isa<StructType>(*TmpI)) {
2185 Out << (HasImplicitAddress ? "." : "->");
2186 Out << "field" << cast<ConstantInt>(TmpI.getOperand())->getZExtValue();
2192 if (isa<StructType>(*I)) {
2193 Out << ".field" << cast<ConstantInt>(I.getOperand())->getZExtValue();
2196 writeOperand(I.getOperand());
2201 void CWriter::visitLoadInst(LoadInst &I) {
2203 if (I.isVolatile()) {
2205 printType(Out, I.getType(), "volatile*");
2209 writeOperand(I.getOperand(0));
2215 void CWriter::visitStoreInst(StoreInst &I) {
2217 if (I.isVolatile()) {
2219 printType(Out, I.getOperand(0)->getType(), " volatile*");
2222 writeOperand(I.getPointerOperand());
2223 if (I.isVolatile()) Out << ')';
2225 writeOperand(I.getOperand(0));
2228 void CWriter::visitGetElementPtrInst(GetElementPtrInst &I) {
2230 printIndexingExpression(I.getPointerOperand(), gep_type_begin(I),
2234 void CWriter::visitVAArgInst(VAArgInst &I) {
2235 Out << "va_arg(*(va_list*)";
2236 writeOperand(I.getOperand(0));
2238 printType(Out, I.getType());
2242 //===----------------------------------------------------------------------===//
2243 // External Interface declaration
2244 //===----------------------------------------------------------------------===//
2246 bool CTargetMachine::addPassesToEmitWholeFile(PassManager &PM,
2248 CodeGenFileType FileType,
2250 if (FileType != TargetMachine::AssemblyFile) return true;
2252 PM.add(createLowerGCPass());
2253 PM.add(createLowerAllocationsPass(true));
2254 PM.add(createLowerInvokePass());
2255 PM.add(createCFGSimplificationPass()); // clean up after lower invoke.
2256 PM.add(new CBackendNameAllUsedStructsAndMergeFunctions());
2257 PM.add(new CWriter(o));