llvm.org GIT mirror llvm / a276c60
Remove MallocInst from LLVM Instructions. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@84299 91177308-0d34-0410-b5e6-96231b3b80d8 Victor Hernandez 10 years ago
25 changed file(s) with 74 addition(s) and 678 deletion(s). Raw diff Collapse all Expand all
115115 // Methods for support type inquiry through isa, cast, and dyn_cast:
116116 static inline bool classof(const UnaryInstruction *) { return true; }
117117 static inline bool classof(const Instruction *I) {
118 return I->getOpcode() == Instruction::Malloc ||
119 I->getOpcode() == Instruction::Alloca ||
118 return I->getOpcode() == Instruction::Alloca ||
120119 I->getOpcode() == Instruction::Free ||
121120 I->getOpcode() == Instruction::Load ||
122121 I->getOpcode() == Instruction::VAArg ||
127127
128128 // Memory operators...
129129 FIRST_MEMORY_INST(25)
130 HANDLE_MEMORY_INST(25, Malloc, MallocInst) // Heap management instructions
131 HANDLE_MEMORY_INST(26, Free , FreeInst )
132 HANDLE_MEMORY_INST(27, Alloca, AllocaInst) // Stack management
133 HANDLE_MEMORY_INST(28, Load , LoadInst ) // Memory manipulation instrs
134 HANDLE_MEMORY_INST(29, Store , StoreInst )
135 HANDLE_MEMORY_INST(30, GetElementPtr, GetElementPtrInst)
136 LAST_MEMORY_INST(30)
130 HANDLE_MEMORY_INST(25, Free , FreeInst ) // Heap management instructions
131 HANDLE_MEMORY_INST(26, Alloca, AllocaInst) // Stack management
132 HANDLE_MEMORY_INST(27, Load , LoadInst ) // Memory manipulation instrs
133 HANDLE_MEMORY_INST(28, Store , StoreInst )
134 HANDLE_MEMORY_INST(29, GetElementPtr, GetElementPtrInst)
135 LAST_MEMORY_INST(29)
137136
138137 // Cast operators ...
139138 // NOTE: The order matters here because CastInst::isEliminableCastPair
140139 // NOTE: (see Instructions.cpp) encodes a table based on this ordering.
141 FIRST_CAST_INST(31)
142 HANDLE_CAST_INST(31, Trunc , TruncInst ) // Truncate integers
143 HANDLE_CAST_INST(32, ZExt , ZExtInst ) // Zero extend integers
144 HANDLE_CAST_INST(33, SExt , SExtInst ) // Sign extend integers
145 HANDLE_CAST_INST(34, FPToUI , FPToUIInst ) // floating point -> UInt
146 HANDLE_CAST_INST(35, FPToSI , FPToSIInst ) // floating point -> SInt
147 HANDLE_CAST_INST(36, UIToFP , UIToFPInst ) // UInt -> floating point
148 HANDLE_CAST_INST(37, SIToFP , SIToFPInst ) // SInt -> floating point
149 HANDLE_CAST_INST(38, FPTrunc , FPTruncInst ) // Truncate floating point
150 HANDLE_CAST_INST(39, FPExt , FPExtInst ) // Extend floating point
151 HANDLE_CAST_INST(40, PtrToInt, PtrToIntInst) // Pointer -> Integer
152 HANDLE_CAST_INST(41, IntToPtr, IntToPtrInst) // Integer -> Pointer
153 HANDLE_CAST_INST(42, BitCast , BitCastInst ) // Type cast
154 LAST_CAST_INST(42)
140 FIRST_CAST_INST(30)
141 HANDLE_CAST_INST(30, Trunc , TruncInst ) // Truncate integers
142 HANDLE_CAST_INST(31, ZExt , ZExtInst ) // Zero extend integers
143 HANDLE_CAST_INST(32, SExt , SExtInst ) // Sign extend integers
144 HANDLE_CAST_INST(33, FPToUI , FPToUIInst ) // floating point -> UInt
145 HANDLE_CAST_INST(34, FPToSI , FPToSIInst ) // floating point -> SInt
146 HANDLE_CAST_INST(35, UIToFP , UIToFPInst ) // UInt -> floating point
147 HANDLE_CAST_INST(36, SIToFP , SIToFPInst ) // SInt -> floating point
148 HANDLE_CAST_INST(37, FPTrunc , FPTruncInst ) // Truncate floating point
149 HANDLE_CAST_INST(38, FPExt , FPExtInst ) // Extend floating point
150 HANDLE_CAST_INST(39, PtrToInt, PtrToIntInst) // Pointer -> Integer
151 HANDLE_CAST_INST(40, IntToPtr, IntToPtrInst) // Integer -> Pointer
152 HANDLE_CAST_INST(41, BitCast , BitCastInst ) // Type cast
153 LAST_CAST_INST(41)
155154
156155 // Other operators...
157 FIRST_OTHER_INST(43)
158 HANDLE_OTHER_INST(43, ICmp , ICmpInst ) // Integer comparison instruction
159 HANDLE_OTHER_INST(44, FCmp , FCmpInst ) // Floating point comparison instr.
160 HANDLE_OTHER_INST(45, PHI , PHINode ) // PHI node instruction
161 HANDLE_OTHER_INST(46, Call , CallInst ) // Call a function
162 HANDLE_OTHER_INST(47, Select , SelectInst ) // select instruction
163 HANDLE_OTHER_INST(48, UserOp1, Instruction) // May be used internally in a pass
164 HANDLE_OTHER_INST(49, UserOp2, Instruction) // Internal to passes only
165 HANDLE_OTHER_INST(50, VAArg , VAArgInst ) // vaarg instruction
166 HANDLE_OTHER_INST(51, ExtractElement, ExtractElementInst)// extract from vector
167 HANDLE_OTHER_INST(52, InsertElement, InsertElementInst) // insert into vector
168 HANDLE_OTHER_INST(53, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
169 HANDLE_OTHER_INST(54, ExtractValue, ExtractValueInst)// extract from aggregate
170 HANDLE_OTHER_INST(55, InsertValue, InsertValueInst) // insert into aggregate
156 FIRST_OTHER_INST(42)
157 HANDLE_OTHER_INST(42, ICmp , ICmpInst ) // Integer comparison instruction
158 HANDLE_OTHER_INST(43, FCmp , FCmpInst ) // Floating point comparison instr.
159 HANDLE_OTHER_INST(44, PHI , PHINode ) // PHI node instruction
160 HANDLE_OTHER_INST(45, Call , CallInst ) // Call a function
161 HANDLE_OTHER_INST(46, Select , SelectInst ) // select instruction
162 HANDLE_OTHER_INST(47, UserOp1, Instruction) // May be used internally in a pass
163 HANDLE_OTHER_INST(48, UserOp2, Instruction) // Internal to passes only
164 HANDLE_OTHER_INST(49, VAArg , VAArgInst ) // vaarg instruction
165 HANDLE_OTHER_INST(50, ExtractElement, ExtractElementInst)// extract from vector
166 HANDLE_OTHER_INST(51, InsertElement, InsertElementInst) // insert into vector
167 HANDLE_OTHER_INST(52, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
168 HANDLE_OTHER_INST(53, ExtractValue, ExtractValueInst)// extract from aggregate
169 HANDLE_OTHER_INST(54, InsertValue, InsertValueInst) // insert into aggregate
171170
172 LAST_OTHER_INST(55)
171 LAST_OTHER_INST(54)
173172
174173 #undef FIRST_TERM_INST
175174 #undef HANDLE_TERM_INST
3636 // AllocationInst Class
3737 //===----------------------------------------------------------------------===//
3838
39 /// AllocationInst - This class is the common base class of MallocInst and
40 /// AllocaInst.
39 /// AllocationInst - This class is the base class of AllocaInst.
4140 ///
4241 class AllocationInst : public UnaryInstruction {
4342 protected:
8483 // Methods for support type inquiry through isa, cast, and dyn_cast:
8584 static inline bool classof(const AllocationInst *) { return true; }
8685 static inline bool classof(const Instruction *I) {
87 return I->getOpcode() == Instruction::Alloca ||
88 I->getOpcode() == Instruction::Malloc;
89 }
90 static inline bool classof(const Value *V) {
91 return isa(V) && classof(cast(V));
92 }
93 };
94
95
96 //===----------------------------------------------------------------------===//
97 // MallocInst Class
98 //===----------------------------------------------------------------------===//
99
100 /// MallocInst - an instruction to allocated memory on the heap
101 ///
102 class MallocInst : public AllocationInst {
103 public:
104 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
105 const Twine &NameStr = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Malloc,
108 0, NameStr, InsertBefore) {}
109 MallocInst(const Type *Ty, Value *ArraySize,
110 const Twine &NameStr, BasicBlock *InsertAtEnd)
111 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
112
113 MallocInst(const Type *Ty, const Twine &NameStr,
114 Instruction *InsertBefore = 0)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
116 MallocInst(const Type *Ty, const Twine &NameStr,
117 BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
119
120 MallocInst(const Type *Ty, Value *ArraySize,
121 unsigned Align, const Twine &NameStr,
122 BasicBlock *InsertAtEnd)
123 : AllocationInst(Ty, ArraySize, Malloc,
124 Align, NameStr, InsertAtEnd) {}
125 MallocInst(const Type *Ty, Value *ArraySize,
126 unsigned Align, const Twine &NameStr = "",
127 Instruction *InsertBefore = 0)
128 : AllocationInst(Ty, ArraySize,
129 Malloc, Align, NameStr, InsertBefore) {}
130
131 virtual MallocInst *clone() const;
132
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const MallocInst *) { return true; }
135 static inline bool classof(const Instruction *I) {
136 return (I->getOpcode() == Instruction::Malloc);
86 return I->getOpcode() == Instruction::Alloca;
13787 }
13888 static inline bool classof(const Value *V) {
13989 return isa(V) && classof(cast(V));
428428 // Instruction creation methods: Memory Instructions
429429 //===--------------------------------------------------------------------===//
430430
431 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
432 const Twine &Name = "") {
433 return Insert(new MallocInst(Ty, ArraySize), Name);
434 }
435431 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
436432 const Twine &Name = "") {
437433 return Insert(new AllocaInst(Ty, ArraySize), Name);
4545 /// /// Declare the class. Note that we derive from InstVisitor instantiated
4646 /// /// with _our new subclasses_ type.
4747 /// ///
48 /// struct CountMallocVisitor : public InstVisitorVisitor> {
48 /// struct CountAllocaVisitor : public InstVisitorVisitor> {
4949 /// unsigned Count;
50 /// CountMallocVisitor() : Count(0) {}
51 ///
52 /// void visitMallocInst(MallocInst &MI) { ++Count; }
50 /// CountAllocaVisitor() : Count(0) {}
51 ///
52 /// void visitAllocaInst(AllocaInst &AI) { ++Count; }
5353 /// };
5454 ///
5555 /// And this class would be used like this:
56 /// CountMallocVistor CMV;
57 /// CMV.visit(function);
58 /// NumMallocs = CMV.Count;
56 /// CountAllocaVisitor CAV;
57 /// CAV.visit(function);
58 /// NumAllocas = CAV.Count;
5959 ///
6060 /// The defined has 'visit' methods for Instruction, and also for BasicBlock,
6161 /// Function, and Module, which recursively process all contained instructions.
164164 RetTy visitUnreachableInst(UnreachableInst &I) { DELEGATE(TerminatorInst);}
165165 RetTy visitICmpInst(ICmpInst &I) { DELEGATE(CmpInst);}
166166 RetTy visitFCmpInst(FCmpInst &I) { DELEGATE(CmpInst);}
167 RetTy visitMallocInst(MallocInst &I) { DELEGATE(AllocationInst);}
168167 RetTy visitAllocaInst(AllocaInst &I) { DELEGATE(AllocationInst);}
169168 RetTy visitFreeInst(FreeInst &I) { DELEGATE(Instruction); }
170169 RetTy visitLoadInst(LoadInst &I) { DELEGATE(Instruction); }
224224
225225 //===----------------------------------------------------------------------===//
226226 //
227 // LowerAllocations - Turn malloc and free instructions into @malloc and @free
228 // calls.
227 // LowerAllocations - Turn free instructions into @free calls.
229228 //
230229 // AU.addRequiredID(LowerAllocationsID);
231230 //
232 Pass *createLowerAllocationsPass(bool LowerMallocArgToInteger = false);
231 Pass *createLowerAllocationsPass();
233232 extern const PassInfo *const LowerAllocationsID;
234233
235234 //===----------------------------------------------------------------------===//
456456 macro(UnaryInstruction) \
457457 macro(AllocationInst) \
458458 macro(AllocaInst) \
459 macro(MallocInst) \
460459 macro(CastInst) \
461460 macro(BitCastInst) \
462461 macro(FPExtInst) \
302302 // Check the value being stored.
303303 Value *Ptr = SI->getOperand(0)->getUnderlyingObject();
304304
305 if (isa(Ptr) || isMalloc(Ptr)) {
305 if (isMalloc(Ptr)) {
306306 // Okay, easy case.
307307 } else if (CallInst *CI = dyn_cast(Ptr)) {
308308 Function *F = CI->getCalledFunction();
438438 if (cast(*II).isVolatile())
439439 // Treat volatile stores as reading memory somewhere.
440440 FunctionEffect |= Ref;
441 } else if (isa(*II) || isa(*II) ||
442 isMalloc(&cast(*II))) {
441 } else if (isMalloc(&cast(*II)) || isa(*II)) {
443442 FunctionEffect |= ModRef;
444443 }
445444
130130 }
131131
132132 // These, too, are calls.
133 if (isa<MallocInst>(II) || isa<FreeInst>(II))
133 if (isa<FreeInst>(II))
134134 NumInsts += InlineConstants::CallPenalty;
135135
136136 if (const AllocaInst *AI = dyn_cast(II)) {
7575 bool InstCount::runOnFunction(Function &F) {
7676 unsigned StartMemInsts =
7777 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
78 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
78 NumInvokeInst + NumAllocaInst + NumFreeInst;
7979 visit(F);
8080 unsigned EndMemInsts =
8181 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
82 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
82 NumInvokeInst + NumAllocaInst + NumFreeInst;
8383 TotalMemInst += EndMemInsts-StartMemInsts;
8484 return false;
8585 }
468468 break;
469469 }
470470
471 case Instruction::Alloca:
472 case Instruction::Malloc: {
471 case Instruction::Alloca: {
473472 AllocationInst *AI = cast(V);
474473 unsigned Align = AI->getAlignment();
475 if (Align == 0 && TD) {
476 if (isa(AI))
477 Align = TD->getABITypeAlignment(AI->getType()->getElementType());
478 else if (isa(AI)) {
479 // Malloc returns maximally aligned memory.
480 Align = TD->getABITypeAlignment(AI->getType()->getElementType());
481 Align =
482 std::max(Align,
483 (unsigned)TD->getABITypeAlignment(
484 Type::getDoubleTy(V->getContext())));
485 Align =
486 std::max(Align,
487 (unsigned)TD->getABITypeAlignment(
488 Type::getInt64Ty(V->getContext())));
489 }
490 }
474 if (Align == 0 && TD)
475 Align = TD->getABITypeAlignment(AI->getType()->getElementType());
491476
492477 if (Align > 0)
493478 KnownZero = Mask & APInt::getLowBitsSet(BitWidth,
33143314 }
33153315
33163316 /// ParsePHI
3317 /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Valueß ']')*
3317 /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Valueß ']')*
33183318 bool LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
33193319 PATypeHolder Ty(Type::getVoidTy(Context));
33203320 Value *Op0, *Op1;
10531053 Vals.push_back(VE.getValueID(I.getOperand(i)));
10541054 break;
10551055
1056 case Instruction::Malloc:
1057 Code = bitc::FUNC_CODE_INST_MALLOC;
1058 Vals.push_back(VE.getTypeID(I.getType()));
1059 Vals.push_back(VE.getValueID(I.getOperand(0))); // size.
1060 Vals.push_back(Log2_32(cast(I).getAlignment())+1);
1061 break;
1062
10631056 case Instruction::Free:
10641057 Code = bitc::FUNC_CODE_INST_FREE;
10651058 PushValueAndType(I.getOperand(0), InstID, Vals, VE);
54845484 DAG.setRoot(Chain);
54855485 }
54865486
5487
5488 void SelectionDAGLowering::visitMalloc(MallocInst &I) {
5489 SDValue Src = getValue(I.getOperand(0));
5490
5491 // Scale up by the type size in the original i32 type width. Various
5492 // mid-level optimizers may make assumptions about demanded bits etc from the
5493 // i32-ness of the optimizer: we do not want to promote to i64 and then
5494 // multiply on 64-bit targets.
5495 // FIXME: Malloc inst should go away: PR715.
5496 uint64_t ElementSize = TD->getTypeAllocSize(I.getType()->getElementType());
5497 if (ElementSize != 1) {
5498 // Src is always 32-bits, make sure the constant fits.
5499 assert(Src.getValueType() == MVT::i32);
5500 ElementSize = (uint32_t)ElementSize;
5501 Src = DAG.getNode(ISD::MUL, getCurDebugLoc(), Src.getValueType(),
5502 Src, DAG.getConstant(ElementSize, Src.getValueType()));
5503 }
5504
5505 EVT IntPtr = TLI.getPointerTy();
5506
5507 Src = DAG.getZExtOrTrunc(Src, getCurDebugLoc(), IntPtr);
5508
5509 TargetLowering::ArgListTy Args;
5510 TargetLowering::ArgListEntry Entry;
5511 Entry.Node = Src;
5512 Entry.Ty = TLI.getTargetData()->getIntPtrType(*DAG.getContext());
5513 Args.push_back(Entry);
5514
5515 bool isTailCall = PerformTailCallOpt &&
5516 isInTailCallPosition(&I, Attribute::None, TLI);
5517 std::pair Result =
5518 TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, false,
5519 0, CallingConv::C, isTailCall,
5520 /*isReturnValueUsed=*/true,
5521 DAG.getExternalSymbol("malloc", IntPtr),
5522 Args, DAG, getCurDebugLoc());
5523 if (Result.first.getNode())
5524 setValue(&I, Result.first); // Pointers always fit in registers
5525 if (Result.second.getNode())
5526 DAG.setRoot(Result.second);
5527 }
5528
55295487 void SelectionDAGLowering::visitFree(FreeInst &I) {
55305488 TargetLowering::ArgListTy Args;
55315489 TargetLowering::ArgListEntry Entry;
5959 class MachineInstr;
6060 class MachineModuleInfo;
6161 class MachineRegisterInfo;
62 class MallocInst;
6362 class PHINode;
6463 class PtrToIntInst;
6564 class ReturnInst;
528527 void visitGetElementPtr(User &I);
529528 void visitSelect(User &I);
530529
531 void visitMalloc(MallocInst &I);
532530 void visitFree(FreeInst &I);
533531 void visitAlloca(AllocaInst &I);
534532 void visitLoad(LoadInst &I);
301301 void visitInlineAsm(CallInst &I);
302302 bool visitBuiltinCall(CallInst &I, Intrinsic::ID ID, bool &WroteCallee);
303303
304 void visitMallocInst(MallocInst &I);
305304 void visitAllocaInst(AllocaInst &I);
306305 void visitFreeInst (FreeInst &I);
307306 void visitLoadInst (LoadInst &I);
34043403 Out << ")";
34053404 }
34063405
3407 void CWriter::visitMallocInst(MallocInst &I) {
3408 llvm_unreachable("lowerallocations pass didn't work!");
3409 }
3410
34113406 void CWriter::visitAllocaInst(AllocaInst &I) {
34123407 Out << '(';
34133408 printType(Out, I.getType());
36893684 if (FileType != TargetMachine::AssemblyFile) return true;
36903685
36913686 PM.add(createGCLoweringPass());
3692 PM.add(createLowerAllocationsPass(true));
3687 PM.add(createLowerAllocationsPass());
36933688 PM.add(createLowerInvokePass());
36943689 PM.add(createCFGSimplificationPass()); // clean up after lower invoke.
36953690 PM.add(new CBackendNameAllUsedStructsAndMergeFunctions());
12571257 Out << "\");";
12581258 break;
12591259 }
1260 case Instruction::Malloc: {
1261 const MallocInst* mallocI = cast(I);
1262 Out << "MallocInst* " << iName << " = new MallocInst("
1263 << getCppName(mallocI->getAllocatedType()) << ", ";
1264 if (mallocI->isArrayAllocation())
1265 Out << opNames[0] << ", " ;
1266 Out << "\"";
1267 printEscapedString(mallocI->getName());
1268 Out << "\", " << bbname << ");";
1269 if (mallocI->getAlignment())
1270 nl(Out) << iName << "->setAlignment("
1271 << mallocI->getAlignment() << ");";
1272 break;
1273 }
12741260 case Instruction::Free: {
12751261 Out << "FreeInst* " << iName << " = new FreeInst("
12761262 << getCppName(I->getOperand(0)) << ", " << bbname << ");";
11901190 case Instruction::Alloca:
11911191 printAllocaInstruction(cast(Inst));
11921192 break;
1193 case Instruction::Malloc:
1194 llvm_unreachable("LowerAllocationsPass used");
1195 break;
11961193 case Instruction::Free:
11971194 llvm_unreachable("LowerAllocationsPass used");
11981195 break;
17011698 if (FileType != TargetMachine::AssemblyFile) return true;
17021699 MSILWriter* Writer = new MSILWriter(o);
17031700 PM.add(createGCLoweringPass());
1704 PM.add(createLowerAllocationsPass(true));
1701 PM.add(createLowerAllocationsPass());
17051702 // FIXME: Handle switch trougth native IL instruction "switch"
17061703 PM.add(createLowerSwitchPass());
17071704 PM.add(createCFGSimplificationPass());
152152 // Writes memory. Just give up.
153153 return false;
154154
155 if (isa(I))
155 if (isMalloc(I))
156156 // malloc claims not to write memory! PR3754.
157157 return false;
158158
266266
267267 // Check whether the pointer came from an allocation.
268268 case Instruction::Alloca:
269 case Instruction::Malloc:
270269 break;
271270 case Instruction::Call:
272271 if (isMalloc(RVI))
821821 /// malloc, there is no reason to actually DO the malloc. Instead, turn the
822822 /// malloc into a global, and any loads of GV as uses of the new global.
823823 static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
824 MallocInst *MI,
825 LLVMContext &Context) {
826 DEBUG(errs() << "PROMOTING MALLOC GLOBAL: " << *GV << " MALLOC = " << *MI);
827 ConstantInt *NElements = cast(MI->getArraySize());
828
829 if (NElements->getZExtValue() != 1) {
830 // If we have an array allocation, transform it to a single element
831 // allocation to make the code below simpler.
832 Type *NewTy = ArrayType::get(MI->getAllocatedType(),
833 NElements->getZExtValue());
834 MallocInst *NewMI =
835 new MallocInst(NewTy, Constant::getNullValue(Type::getInt32Ty(Context)),
836 MI->getAlignment(), MI->getName(), MI);
837 Value* Indices[2];
838 Indices[0] = Indices[1] = Constant::getNullValue(Type::getInt32Ty(Context));
839 Value *NewGEP = GetElementPtrInst::Create(NewMI, Indices, Indices + 2,
840 NewMI->getName()+".el0", MI);
841 MI->replaceAllUsesWith(NewGEP);
842 MI->eraseFromParent();
843 MI = NewMI;
844 }
845
846 // Create the new global variable. The contents of the malloc'd memory is
847 // undefined, so initialize with an undef value.
848 // FIXME: This new global should have the alignment returned by malloc. Code
849 // could depend on malloc returning large alignment (on the mac, 16 bytes) but
850 // this would only guarantee some lower alignment.
851 Constant *Init = UndefValue::get(MI->getAllocatedType());
852 GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
853 MI->getAllocatedType(), false,
854 GlobalValue::InternalLinkage, Init,
855 GV->getName()+".body",
856 GV,
857 GV->isThreadLocal());
858
859 // Anything that used the malloc now uses the global directly.
860 MI->replaceAllUsesWith(NewGV);
861
862 Constant *RepValue = NewGV;
863 if (NewGV->getType() != GV->getType()->getElementType())
864 RepValue = ConstantExpr::getBitCast(RepValue,
865 GV->getType()->getElementType());
866
867 // If there is a comparison against null, we will insert a global bool to
868 // keep track of whether the global was initialized yet or not.
869 GlobalVariable *InitBool =
870 new GlobalVariable(Context, Type::getInt1Ty(Context), false,
871 GlobalValue::InternalLinkage,
872 ConstantInt::getFalse(Context), GV->getName()+".init",
873 GV->isThreadLocal());
874 bool InitBoolUsed = false;
875
876 // Loop over all uses of GV, processing them in turn.
877 std::vector Stores;
878 while (!GV->use_empty())
879 if (LoadInst *LI = dyn_cast(GV->use_back())) {
880 while (!LI->use_empty()) {
881 Use &LoadUse = LI->use_begin().getUse();
882 if (!isa(LoadUse.getUser()))
883 LoadUse = RepValue;
884 else {
885 ICmpInst *CI = cast(LoadUse.getUser());
886 // Replace the cmp X, 0 with a use of the bool value.
887 Value *LV = new LoadInst(InitBool, InitBool->getName()+".val", CI);
888 InitBoolUsed = true;
889 switch (CI->getPredicate()) {
890 default: llvm_unreachable("Unknown ICmp Predicate!");
891 case ICmpInst::ICMP_ULT:
892 case ICmpInst::ICMP_SLT:
893 LV = ConstantInt::getFalse(Context); // X < null -> always false
894 break;
895 case ICmpInst::ICMP_ULE:
896 case ICmpInst::ICMP_SLE:
897 case ICmpInst::ICMP_EQ:
898 LV = BinaryOperator::CreateNot(LV, "notinit", CI);
899 break;
900 case ICmpInst::ICMP_NE:
901 case ICmpInst::ICMP_UGE:
902 case ICmpInst::ICMP_SGE:
903 case ICmpInst::ICMP_UGT:
904 case ICmpInst::ICMP_SGT:
905 break; // no change.
906 }
907 CI->replaceAllUsesWith(LV);
908 CI->eraseFromParent();
909 }
910 }
911 LI->eraseFromParent();
912 } else {
913 StoreInst *SI = cast(GV->use_back());
914 // The global is initialized when the store to it occurs.
915 new StoreInst(ConstantInt::getTrue(Context), InitBool, SI);
916 SI->eraseFromParent();
917 }
918
919 // If the initialization boolean was used, insert it, otherwise delete it.
920 if (!InitBoolUsed) {
921 while (!InitBool->use_empty()) // Delete initializations
922 cast(InitBool->use_back())->eraseFromParent();
923 delete InitBool;
924 } else
925 GV->getParent()->getGlobalList().insert(GV, InitBool);
926
927
928 // Now the GV is dead, nuke it and the malloc.
929 GV->eraseFromParent();
930 MI->eraseFromParent();
931
932 // To further other optimizations, loop over all users of NewGV and try to
933 // constant prop them. This will promote GEP instructions with constant
934 // indices into GEP constant-exprs, which will allow global-opt to hack on it.
935 ConstantPropUsersOf(NewGV, Context);
936 if (RepValue != NewGV)
937 ConstantPropUsersOf(RepValue, Context);
938
939 return NewGV;
940 }
941
942 /// OptimizeGlobalAddressOfMalloc - This function takes the specified global
943 /// variable, and transforms the program as if it always contained the result of
944 /// the specified malloc. Because it is always the result of the specified
945 /// malloc, there is no reason to actually DO the malloc. Instead, turn the
946 /// malloc into a global, and any loads of GV as uses of the new global.
947 static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV,
948824 CallInst *CI,
949825 BitCastInst *BCI,
950826 LLVMContext &Context,
13961272 }
13971273 }
13981274
1399 /// PerformHeapAllocSRoA - MI is an allocation of an array of structures. Break
1400 /// it up into multiple allocations of arrays of the fields.
1401 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, MallocInst *MI,
1402 LLVMContext &Context){
1403 DEBUG(errs() << "SROA HEAP ALLOC: " << *GV << " MALLOC = " << *MI);
1404 const StructType *STy = cast(MI->getAllocatedType());
1405
1406 // There is guaranteed to be at least one use of the malloc (storing
1407 // it into GV). If there are other uses, change them to be uses of
1408 // the global to simplify later code. This also deletes the store
1409 // into GV.
1410 ReplaceUsesOfMallocWithGlobal(MI, GV);
1411
1412 // Okay, at this point, there are no users of the malloc. Insert N
1413 // new mallocs at the same place as MI, and N globals.
1414 std::vector FieldGlobals;
1415 std::vector FieldMallocs;
1416
1417 for (unsigned FieldNo = 0, e = STy->getNumElements(); FieldNo != e;++FieldNo){
1418 const Type *FieldTy = STy->getElementType(FieldNo);
1419 const Type *PFieldTy = PointerType::getUnqual(FieldTy);
1420
1421 GlobalVariable *NGV =
1422 new GlobalVariable(*GV->getParent(),
1423 PFieldTy, false, GlobalValue::InternalLinkage,
1424 Constant::getNullValue(PFieldTy),
1425 GV->getName() + ".f" + Twine(FieldNo), GV,
1426 GV->isThreadLocal());
1427 FieldGlobals.push_back(NGV);
1428
1429 MallocInst *NMI = new MallocInst(FieldTy, MI->getArraySize(),
1430 MI->getName() + ".f" + Twine(FieldNo), MI);
1431 FieldMallocs.push_back(NMI);
1432 new StoreInst(NMI, NGV, MI);
1433 }
1434
1435 // The tricky aspect of this transformation is handling the case when malloc
1436 // fails. In the original code, malloc failing would set the result pointer
1437 // of malloc to null. In this case, some mallocs could succeed and others
1438 // could fail. As such, we emit code that looks like this:
1439 // F0 = malloc(field0)
1440 // F1 = malloc(field1)
1441 // F2 = malloc(field2)
1442 // if (F0 == 0 || F1 == 0 || F2 == 0) {
1443 // if (F0) { free(F0); F0 = 0; }
1444 // if (F1) { free(F1); F1 = 0; }
1445 // if (F2) { free(F2); F2 = 0; }
1446 // }
1447 Value *RunningOr = 0;
1448 for (unsigned i = 0, e = FieldMallocs.size(); i != e; ++i) {
1449 Value *Cond = new ICmpInst(MI, ICmpInst::ICMP_EQ, FieldMallocs[i],
1450 Constant::getNullValue(FieldMallocs[i]->getType()),
1451 "isnull");
1452 if (!RunningOr)
1453 RunningOr = Cond; // First seteq
1454 else
1455 RunningOr = BinaryOperator::CreateOr(RunningOr, Cond, "tmp", MI);
1456 }
1457
1458 // Split the basic block at the old malloc.
1459 BasicBlock *OrigBB = MI->getParent();
1460 BasicBlock *ContBB = OrigBB->splitBasicBlock(MI, "malloc_cont");
1461
1462 // Create the block to check the first condition. Put all these blocks at the
1463 // end of the function as they are unlikely to be executed.
1464 BasicBlock *NullPtrBlock = BasicBlock::Create(Context, "malloc_ret_null",
1465 OrigBB->getParent());
1466
1467 // Remove the uncond branch from OrigBB to ContBB, turning it into a cond
1468 // branch on RunningOr.
1469 OrigBB->getTerminator()->eraseFromParent();
1470 BranchInst::Create(NullPtrBlock, ContBB, RunningOr, OrigBB);
1471
1472 // Within the NullPtrBlock, we need to emit a comparison and branch for each
1473 // pointer, because some may be null while others are not.
1474 for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
1475 Value *GVVal = new LoadInst(FieldGlobals[i], "tmp", NullPtrBlock);
1476 Value *Cmp = new ICmpInst(*NullPtrBlock, ICmpInst::ICMP_NE, GVVal,
1477 Constant::getNullValue(GVVal->getType()),
1478 "tmp");
1479 BasicBlock *FreeBlock = BasicBlock::Create(Context, "free_it",
1480 OrigBB->getParent());
1481 BasicBlock *NextBlock = BasicBlock::Create(Context, "next",
1482 OrigBB->getParent());
1483 BranchInst::Create(FreeBlock, NextBlock, Cmp, NullPtrBlock);
1484
1485 // Fill in FreeBlock.
1486 new FreeInst(GVVal, FreeBlock);
1487 new StoreInst(Constant::getNullValue(GVVal->getType()), FieldGlobals[i],
1488 FreeBlock);
1489 BranchInst::Create(NextBlock, FreeBlock);
1490
1491 NullPtrBlock = NextBlock;
1492 }
1493
1494 BranchInst::Create(ContBB, NullPtrBlock);
1495
1496 // MI is no longer needed, remove it.
1497 MI->eraseFromParent();
1498
1499 /// InsertedScalarizedLoads - As we process loads, if we can't immediately
1500 /// update all uses of the load, keep track of what scalarized loads are
1501 /// inserted for a given load.
1502 DenseMap > InsertedScalarizedValues;
1503 InsertedScalarizedValues[GV] = FieldGlobals;
1504
1505 std::vector > PHIsToRewrite;
1506
1507 // Okay, the malloc site is completely handled. All of the uses of GV are now
1508 // loads, and all uses of those loads are simple. Rewrite them to use loads
1509 // of the per-field globals instead.
1510 for (Value::use_iterator UI = GV->use_begin(), E = GV->use_end(); UI != E;) {
1511 Instruction *User = cast(*UI++);
1512
1513 if (LoadInst *LI = dyn_cast(User)) {
1514 RewriteUsesOfLoadForHeapSRoA(LI, InsertedScalarizedValues, PHIsToRewrite,
1515 Context);
1516 continue;
1517 }
1518
1519 // Must be a store of null.
1520 StoreInst *SI = cast(User);
1521 assert(isa(SI->getOperand(0)) &&
1522 "Unexpected heap-sra user!");
1523
1524 // Insert a store of null into each global.
1525 for (unsigned i = 0, e = FieldGlobals.size(); i != e; ++i) {
1526 const PointerType *PT = cast(FieldGlobals[i]->getType());
1527 Constant *Null = Constant::getNullValue(PT->getElementType());
1528 new StoreInst(Null, FieldGlobals[i], SI);
1529 }
1530 // Erase the original store.
1531 SI->eraseFromParent();
1532 }
1533
1534 // While we have PHIs that are interesting to rewrite, do it.
1535 while (!PHIsToRewrite.empty()) {
1536 PHINode *PN = PHIsToRewrite.back().first;
1537 unsigned FieldNo = PHIsToRewrite.back().second;
1538 PHIsToRewrite.pop_back();
1539 PHINode *FieldPN = cast(InsertedScalarizedValues[PN][FieldNo]);
1540 assert(FieldPN->getNumIncomingValues() == 0 &&"Already processed this phi");
1541
1542 // Add all the incoming values. This can materialize more phis.
1543 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1544 Value *InVal = PN->getIncomingValue(i);
1545 InVal = GetHeapSROAValue(InVal, FieldNo, InsertedScalarizedValues,
1546 PHIsToRewrite, Context);
1547 FieldPN->addIncoming(InVal, PN->getIncomingBlock(i));
1548 }
1549 }
1550
1551 // Drop all inter-phi links and any loads that made it this far.
1552 for (DenseMap >::iterator
1553 I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
1554 I != E; ++I) {
1555 if (PHINode *PN = dyn_cast(I->first))
1556 PN->dropAllReferences();
1557 else if (LoadInst *LI = dyn_cast(I->first))
1558 LI->dropAllReferences();
1559 }
1560
1561 // Delete all the phis and loads now that inter-references are dead.
1562 for (DenseMap >::iterator
1563 I = InsertedScalarizedValues.begin(), E = InsertedScalarizedValues.end();
1564 I != E; ++I) {
1565 if (PHINode *PN = dyn_cast(I->first))
1566 PN->eraseFromParent();
1567 else if (LoadInst *LI = dyn_cast(I->first))
1568 LI->eraseFromParent();
1569 }
1570
1571 // The old global is now dead, remove it.
1572 GV->eraseFromParent();
1573
1574 ++NumHeapSRA;
1575 return cast(FieldGlobals[0]);
1576 }
1577
15781275 /// PerformHeapAllocSRoA - CI is an allocation of an array of structures. Break
15791276 /// it up into multiple allocations of arrays of the fields.
15801277 static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV,
17661463 /// pointer global variable with a single value stored it that is a malloc or
17671464 /// cast of malloc.
17681465 static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
1769 MallocInst *MI,
1770 Module::global_iterator &GVI,
1771 TargetData *TD,
1772 LLVMContext &Context) {
1773 // If this is a malloc of an abstract type, don't touch it.
1774 if (!MI->getAllocatedType()->isSized())
1775 return false;
1776
1777 // We can't optimize this global unless all uses of it are *known* to be
1778 // of the malloc value, not of the null initializer value (consider a use
1779 // that compares the global's value against zero to see if the malloc has
1780 // been reached). To do this, we check to see if all uses of the global
1781 // would trap if the global were null: this proves that they must all
1782 // happen after the malloc.
1783 if (!AllUsesOfLoadedValueWillTrapIfNull(GV))
1784 return false;
1785
1786 // We can't optimize this if the malloc itself is used in a complex way,
1787 // for example, being stored into multiple globals. This allows the
1788 // malloc to be stored into the specified global, loaded setcc'd, and
1789 // GEP'd. These are all things we could transform to using the global
1790 // for.
1791 {
1792 SmallPtrSet PHIs;
1793 if (!ValueIsOnlyUsedLocallyOrStoredToOneGlobal(MI, GV, PHIs))
1794 return false;
1795 }
1796
1797
1798 // If we have a global that is only initialized with a fixed size malloc,
1799 // transform the program to use global memory instead of malloc'd memory.
1800 // This eliminates dynamic allocation, avoids an indirection accessing the
1801 // data, and exposes the resultant global to further GlobalOpt.
1802 if (ConstantInt *NElements = dyn_cast(MI->getArraySize())) {
1803 // Restrict this transformation to only working on small allocations
1804 // (2048 bytes currently), as we don't want to introduce a 16M global or
1805 // something.
1806 if (TD &&
1807 NElements->getZExtValue()*
1808 TD->getTypeAllocSize(MI->getAllocatedType()) < 2048) {
1809 GVI = OptimizeGlobalAddressOfMalloc(GV, MI, Context);
1810 return true;
1811 }
1812 }
1813
1814 // If the allocation is an array of structures, consider transforming this
1815 // into multiple malloc'd arrays, one for each field. This is basically
1816 // SRoA for malloc'd memory.
1817 const Type *AllocTy = MI->getAllocatedType();
1818
1819 // If this is an allocation of a fixed size array of structs, analyze as a
1820 // variable size array. malloc [100 x struct],1 -> malloc struct, 100
1821 if (!MI->isArrayAllocation())
1822 if (const ArrayType *AT = dyn_cast(AllocTy))
1823 AllocTy = AT->getElementType();
1824
1825 if (const StructType *AllocSTy = dyn_cast(AllocTy)) {
1826 // This the structure has an unreasonable number of fields, leave it
1827 // alone.
1828 if (AllocSTy->getNumElements() <= 16 && AllocSTy->getNumElements() != 0 &&
1829 AllGlobalLoadUsesSimpleEnoughForHeapSRA(GV, MI)) {
1830
1831 // If this is a fixed size array, transform the Malloc to be an alloc of
1832 // structs. malloc [100 x struct],1 -> malloc struct, 100
1833 if (const ArrayType *AT = dyn_cast(MI->getAllocatedType())) {
1834 MallocInst *NewMI =
1835 new MallocInst(AllocSTy,
1836 ConstantInt::get(Type::getInt32Ty(Context),
1837 AT->getNumElements()),
1838 "", MI);
1839 NewMI->takeName(MI);
1840 Value *Cast = new BitCastInst(NewMI, MI->getType(), "tmp", MI);
1841 MI->replaceAllUsesWith(Cast);
1842 MI->eraseFromParent();
1843 MI = NewMI;
1844 }
1845
1846 GVI = PerformHeapAllocSRoA(GV, MI, Context);
1847 return true;
1848 }
1849 }
1850
1851 return false;
1852 }
1853
1854 /// TryToOptimizeStoreOfMallocToGlobal - This function is called when we see a
1855 /// pointer global variable with a single value stored it that is a malloc or
1856 /// cast of malloc.
1857 static bool TryToOptimizeStoreOfMallocToGlobal(GlobalVariable *GV,
18581466 CallInst *CI,
18591467 BitCastInst *BCI,
18601468 Module::global_iterator &GVI,
19681576
19691577 // Optimize away any trapping uses of the loaded value.
19701578 if (OptimizeAwayTrappingUsesOfLoads(GV, SOVC, Context))
1971 return true;
1972 } else if (MallocInst *MI = dyn_cast(StoredOnceVal)) {
1973 if (TryToOptimizeStoreOfMallocToGlobal(GV, MI, GVI, TD, Context))
19741579 return true;
19751580 } else if (CallInst *CI = extractMallocCall(StoredOnceVal)) {
19761581 if (getMallocAllocatedType(CI)) {
62996299 return SelectInst::Create(LHSI->getOperand(0), Op1, Op2);
63006300 break;
63016301 }
6302 case Instruction::Malloc:
6303 // If we have (malloc != null), and if the malloc has a single use, we
6304 // can assume it is successful and remove the malloc.
6305 if (LHSI->hasOneUse() && isa(RHSC)) {
6306 Worklist.Add(LHSI);
6307 return ReplaceInstUsesWith(I,
6308 ConstantInt::get(Type::getInt1Ty(*Context),
6309 !I.isTrueWhenEqual()));
6310 }
6311 break;
63126302 case Instruction::Call:
63136303 // If we have (malloc != null), and if the malloc has a single use, we
63146304 // can assume it is successful and remove the malloc.
78087798 Amt = AllocaBuilder.CreateAdd(Amt, Off, "tmp");
78097799 }
78107800
7811 AllocationInst *New;
7812 if (isa(AI))
7813 New = AllocaBuilder.CreateMalloc(CastElTy, Amt);
7814 else
7815 New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
7801 AllocationInst *New = AllocaBuilder.CreateAlloca(CastElTy, Amt);
78167802 New->setAlignment(AI.getAlignment());
78177803 New->takeName(&AI);
78187804
1121211198 if (const ConstantInt *C = dyn_cast(AI.getArraySize())) {
1121311199 const Type *NewTy =
1121411200 ArrayType::get(AI.getAllocatedType(), C->getZExtValue());
11215 AllocationInst *New = 0;
11216
11217 // Create and insert the replacement instruction...
11218 if (isa(AI))
11219 New = Builder->CreateMalloc(NewTy, 0, AI.getName());
11220 else {
11221 assert(isa(AI) && "Unknown type of allocation inst!");
11222 New = Builder->CreateAlloca(NewTy, 0, AI.getName());
11223 }
11201 assert(isa(AI) && "Unknown type of allocation inst!");
11202 AllocationInst *New = Builder->CreateAlloca(NewTy, 0, AI.getName());
1122411203 New->setAlignment(AI.getAlignment());
1122511204
1122611205 // Scan to the end of the allocation instructions, to skip over a block of
1129311272 }
1129411273 }
1129511274
11296 // Change free(malloc) into nothing, if the malloc has a single use.
11297 if (MallocInst *MI = dyn_cast(Op))
11298 if (MI->hasOneUse()) {
11299 EraseInstFromFunction(FI);
11300 return EraseInstFromFunction(*MI);
11301 }
1130211275 if (isMalloc(Op)) {
1130311276 if (CallInst* CI = extractMallocCallFromBitCast(Op)) {
1130411277 if (Op->hasOneUse() && CI->hasOneUse()) {
121121 if (I->getOpcode() == Instruction::PHI ||
122122 I->getOpcode() == Instruction::Alloca ||
123123 I->getOpcode() == Instruction::Load ||
124 I->getOpcode() == Instruction::Malloc || isMalloc(I) ||
124 isMalloc(I) ||
125125 I->getOpcode() == Instruction::Invoke ||
126126 (I->getOpcode() == Instruction::Call &&
127127 !isa(I)) ||
None //===- LowerAllocations.cpp - Reduce malloc & free insts to calls ---------===//
0 //===- LowerAllocations.cpp - Reduce free insts to calls ------------------===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
2828 STATISTIC(NumLowered, "Number of allocations lowered");
2929
3030 namespace {
31 /// LowerAllocations - Turn malloc and free instructions into @malloc and
32 /// @free calls.
31 /// LowerAllocations - Turn free instructions into @free calls.
3332 ///
3433 class VISIBILITY_HIDDEN LowerAllocations : public BasicBlockPass {
3534 Constant *FreeFunc; // Functions in the module we are processing
3635 // Initialized by doInitialization
37 bool LowerMallocArgToInteger;
3836 public:
3937 static char ID; // Pass ID, replacement for typeid
40 explicit LowerAllocations(bool LowerToInt = false)
41 : BasicBlockPass(&ID), FreeFunc(0),
42 LowerMallocArgToInteger(LowerToInt) {}
38 explicit LowerAllocations()
39 : BasicBlockPass(&ID), FreeFunc(0) {}
4340
4441 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
4542 AU.addRequired();
5350 }
5451
5552 /// doPassInitialization - For the lower allocations pass, this ensures that
56 /// a module contains a declaration for a malloc and a free function.
53 /// a module contains a declaration for a free function.
5754 ///
5855 bool doInitialization(Module &M);
5956
7572 // Publically exposed interface to pass...
7673 const PassInfo *const llvm::LowerAllocationsID = &X;
7774 // createLowerAllocationsPass - Interface to this file...
78 Pass *llvm::createLowerAllocationsPass(bool LowerMallocArgToInteger) {
79 return new LowerAllocations(LowerMallocArgToInteger);
75 Pass *llvm::createLowerAllocationsPass() {
76 return new LowerAllocations();
8077 }
8178
8279
8380 // doInitialization - For the lower allocations pass, this ensures that a
84 // module contains a declaration for a malloc and a free function.
81 // module contains a declaration for a free function.
8582 //
8683 // This function is always successful.
8784 //
10198
10299 BasicBlock::InstListType &BBIL = BB.getInstList();
103100
104 const TargetData &TD = getAnalysis();
105 const Type *IntPtrTy = TD.getIntPtrType(BB.getContext());
106
107 // Loop over all of the instructions, looking for malloc or free instructions
101 // Loop over all of the instructions, looking for free instructions
108102 for (BasicBlock::iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
109 if (MallocInst *MI = dyn_cast(I)) {
110 Value *ArraySize = MI->getOperand(0);
111 if (ArraySize->getType() != IntPtrTy)
112 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy,
113 false /*ZExt*/, "", I);
114 Value *MCast = CallInst::CreateMalloc(I, IntPtrTy,
115 MI->getAllocatedType(), ArraySize);
116
117 // Replace all uses of the old malloc inst with the cast inst
118 MI->replaceAllUsesWith(MCast);
119 I = --BBIL.erase(I); // remove and delete the malloc instr...
120 Changed = true;
121 ++NumLowered;
122 } else if (FreeInst *FI = dyn_cast(I)) {
103 if (FreeInst *FI = dyn_cast(I)) {
123104 Value *PtrCast =
124105 new BitCastInst(FI->getOperand(0),
125106 Type::getInt8PtrTy(BB.getContext()), "", I);
126126 case Xor: return "xor";
127127
128128 // Memory instructions...
129 case Malloc: return "malloc";
130129 case Free: return "free";
131130 case Alloca: return "alloca";
132131 case Load: return "load";
441440 // overflow-checking arithmetic, etc.)
442441 case VAArg:
443442 case Alloca:
444 case Malloc:
445443 case Invoke:
446444 case PHI:
447445 case Store:
846846 assert(!isa(Amt) &&
847847 "Passed basic block into allocation size parameter! Use other ctor");
848848 assert(Amt->getType() == Type::getInt32Ty(Context) &&
849 "Malloc/Allocation array size is not a 32-bit integer!");
849 "Allocation array size is not a 32-bit integer!");
850850 }
851851 return Amt;
852852 }
30823082 return New;
30833083 }
30843084
3085 MallocInst *MallocInst::clone() const {
3086 MallocInst *New = new MallocInst(getAllocatedType(),
3087 (Value*)getOperand(0),
3088 getAlignment());
3089 New->SubclassOptionalData = SubclassOptionalData;
3090 if (hasMetadata()) {
3091 LLVMContext &Context = getContext();
3092 Context.pImpl->TheMetadata.ValueIsCloned(this, New);
3093 }
3094 return New;
3095 }
3096
30973085 AllocaInst *AllocaInst::clone() const {
30983086 AllocaInst *New = new AllocaInst(getAllocatedType(),
30993087 (Value*)getOperand(0),