@@ -809,131 +809,6 @@ void CallInst::updateProfWeight(uint64_t S, uint64_t T) {
809809 setMetadata (LLVMContext::MD_prof, MDNode::get (getContext (), Vals));
810810}
811811
812- // / IsConstantOne - Return true only if val is constant int 1
813- static bool IsConstantOne (Value *val) {
814- assert (val && " IsConstantOne does not work with nullptr val" );
815- const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
816- return CVal && CVal->isOne ();
817- }
818-
819- static Instruction *createMalloc (Instruction *InsertBefore,
820- BasicBlock *InsertAtEnd, Type *IntPtrTy,
821- Type *AllocTy, Value *AllocSize,
822- Value *ArraySize,
823- ArrayRef<OperandBundleDef> OpB,
824- Function *MallocF, const Twine &Name) {
825- assert (((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
826- " createMalloc needs either InsertBefore or InsertAtEnd" );
827-
828- // malloc(type) becomes:
829- // bitcast (i8* malloc(typeSize)) to type*
830- // malloc(type, arraySize) becomes:
831- // bitcast (i8* malloc(typeSize*arraySize)) to type*
832- if (!ArraySize)
833- ArraySize = ConstantInt::get (IntPtrTy, 1 );
834- else if (ArraySize->getType () != IntPtrTy) {
835- if (InsertBefore)
836- ArraySize = CastInst::CreateIntegerCast (ArraySize, IntPtrTy, false ,
837- " " , InsertBefore);
838- else
839- ArraySize = CastInst::CreateIntegerCast (ArraySize, IntPtrTy, false ,
840- " " , InsertAtEnd);
841- }
842-
843- if (!IsConstantOne (ArraySize)) {
844- if (IsConstantOne (AllocSize)) {
845- AllocSize = ArraySize; // Operand * 1 = Operand
846- } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
847- Constant *Scale = ConstantExpr::getIntegerCast (CO, IntPtrTy,
848- false /* ZExt*/ );
849- // Malloc arg is constant product of type size and array size
850- AllocSize = ConstantExpr::getMul (Scale, cast<Constant>(AllocSize));
851- } else {
852- // Multiply type size by the array size...
853- if (InsertBefore)
854- AllocSize = BinaryOperator::CreateMul (ArraySize, AllocSize,
855- " mallocsize" , InsertBefore);
856- else
857- AllocSize = BinaryOperator::CreateMul (ArraySize, AllocSize,
858- " mallocsize" , InsertAtEnd);
859- }
860- }
861-
862- assert (AllocSize->getType () == IntPtrTy && " malloc arg is wrong size" );
863- // Create the call to Malloc.
864- BasicBlock *BB = InsertBefore ? InsertBefore->getParent () : InsertAtEnd;
865- Module *M = BB->getParent ()->getParent ();
866- Type *BPTy = PointerType::getUnqual (BB->getContext ());
867- FunctionCallee MallocFunc = MallocF;
868- if (!MallocFunc)
869- // prototype malloc as "void *malloc(size_t)"
870- MallocFunc = M->getOrInsertFunction (" malloc" , BPTy, IntPtrTy);
871- CallInst *MCall = nullptr ;
872- if (InsertBefore) {
873- MCall = CallInst::Create (MallocFunc, AllocSize, OpB, Name,
874- InsertBefore);
875- } else {
876- MCall = CallInst::Create (MallocFunc, AllocSize, OpB, Name);
877- }
878- MCall->setTailCall ();
879- if (Function *F = dyn_cast<Function>(MallocFunc.getCallee ())) {
880- MCall->setCallingConv (F->getCallingConv ());
881- if (!F->returnDoesNotAlias ())
882- F->setReturnDoesNotAlias ();
883- }
884- assert (!MCall->getType ()->isVoidTy () && " Malloc has void return type" );
885-
886- return MCall;
887- }
888-
889- // / CreateMalloc - Generate the IR for a call to malloc:
890- // / 1. Compute the malloc call's argument as the specified type's size,
891- // / possibly multiplied by the array size if the array size is not
892- // / constant 1.
893- // / 2. Call malloc with that argument.
894- // / 3. Bitcast the result of the malloc call to the specified type.
895- Instruction *CallInst::CreateMalloc (Instruction *InsertBefore,
896- Type *IntPtrTy, Type *AllocTy,
897- Value *AllocSize, Value *ArraySize,
898- Function *MallocF,
899- const Twine &Name) {
900- return createMalloc (InsertBefore, nullptr , IntPtrTy, AllocTy, AllocSize,
901- ArraySize, std::nullopt , MallocF, Name);
902- }
903- Instruction *CallInst::CreateMalloc (Instruction *InsertBefore,
904- Type *IntPtrTy, Type *AllocTy,
905- Value *AllocSize, Value *ArraySize,
906- ArrayRef<OperandBundleDef> OpB,
907- Function *MallocF,
908- const Twine &Name) {
909- return createMalloc (InsertBefore, nullptr , IntPtrTy, AllocTy, AllocSize,
910- ArraySize, OpB, MallocF, Name);
911- }
912-
913- // / CreateMalloc - Generate the IR for a call to malloc:
914- // / 1. Compute the malloc call's argument as the specified type's size,
915- // / possibly multiplied by the array size if the array size is not
916- // / constant 1.
917- // / 2. Call malloc with that argument.
918- // / 3. Bitcast the result of the malloc call to the specified type.
919- // / Note: This function does not add the bitcast to the basic block, that is the
920- // / responsibility of the caller.
921- Instruction *CallInst::CreateMalloc (BasicBlock *InsertAtEnd,
922- Type *IntPtrTy, Type *AllocTy,
923- Value *AllocSize, Value *ArraySize,
924- Function *MallocF, const Twine &Name) {
925- return createMalloc (nullptr , InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
926- ArraySize, std::nullopt , MallocF, Name);
927- }
928- Instruction *CallInst::CreateMalloc (BasicBlock *InsertAtEnd,
929- Type *IntPtrTy, Type *AllocTy,
930- Value *AllocSize, Value *ArraySize,
931- ArrayRef<OperandBundleDef> OpB,
932- Function *MallocF, const Twine &Name) {
933- return createMalloc (nullptr , InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
934- ArraySize, OpB, MallocF, Name);
935- }
936-
937812static Instruction *createFree (Value *Source,
938813 ArrayRef<OperandBundleDef> Bundles,
939814 Instruction *InsertBefore,
0 commit comments