blob: 4ae2fd522b52a5d3b81653177dd93f3e44c72907 [file] [log] [blame]
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00008//===----------------------------------------------------------------------===//
9//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000010// This file implements all of the non-inline methods for the LLVM instruction
11// classes.
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/Instructions.h"
Devang Pateladd58652009-09-23 18:32:25 +000016#include "LLVMContextImpl.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000017#include "llvm/IR/CallSite.h"
Chandler Carruth8cd041e2014-03-04 12:24:34 +000018#include "llvm/IR/ConstantRange.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Constants.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/Module.h"
24#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ErrorHandling.h"
Christopher Lamb84485702007-04-22 19:24:39 +000026#include "llvm/Support/MathExtras.h"
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +000027using namespace llvm;
28
Chris Lattner3e13b8c2008-01-02 23:42:30 +000029//===----------------------------------------------------------------------===//
30// CallSite Class
31//===----------------------------------------------------------------------===//
32
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000033User::op_iterator CallSite::getCallee() const {
34 Instruction *II(getInstruction());
35 return isCall()
Gabor Greif638c8232010-08-05 21:25:49 +000036 ? cast<CallInst>(II)->op_end() - 1 // Skip Callee
Gabor Greif6d673952010-07-16 09:38:02 +000037 : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Callee
Gabor Greifa2fbc0a2010-03-24 13:21:49 +000038}
39
Gordon Henriksen14a55692007-12-10 02:14:30 +000040//===----------------------------------------------------------------------===//
41// TerminatorInst Class
42//===----------------------------------------------------------------------===//
43
44// Out of line virtual method, so the vtable, etc has a home.
45TerminatorInst::~TerminatorInst() {
46}
47
Gabor Greiff6caff662008-05-10 08:32:32 +000048//===----------------------------------------------------------------------===//
49// UnaryInstruction Class
50//===----------------------------------------------------------------------===//
51
Gordon Henriksen14a55692007-12-10 02:14:30 +000052// Out of line virtual method, so the vtable, etc has a home.
53UnaryInstruction::~UnaryInstruction() {
54}
55
Chris Lattnerafdb3de2005-01-29 00:35:16 +000056//===----------------------------------------------------------------------===//
Chris Lattner88107952008-12-29 00:12:50 +000057// SelectInst Class
58//===----------------------------------------------------------------------===//
59
60/// areInvalidOperands - Return a string if the specified operands are invalid
61/// for a select operation, otherwise return null.
62const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) {
63 if (Op1->getType() != Op2->getType())
64 return "both values to select must have same type";
David Majnemerb611e3f2015-08-14 05:09:07 +000065
66 if (Op1->getType()->isTokenTy())
67 return "select values cannot have token type";
68
Chris Lattner229907c2011-07-18 04:54:35 +000069 if (VectorType *VT = dyn_cast<VectorType>(Op0->getType())) {
Chris Lattner88107952008-12-29 00:12:50 +000070 // Vector select.
Owen Anderson55f1c092009-08-13 21:58:54 +000071 if (VT->getElementType() != Type::getInt1Ty(Op0->getContext()))
Chris Lattner88107952008-12-29 00:12:50 +000072 return "vector select condition element type must be i1";
Chris Lattner229907c2011-07-18 04:54:35 +000073 VectorType *ET = dyn_cast<VectorType>(Op1->getType());
Craig Topperc6207612014-04-09 06:08:46 +000074 if (!ET)
Chris Lattner88107952008-12-29 00:12:50 +000075 return "selected values for vector select must be vectors";
76 if (ET->getNumElements() != VT->getNumElements())
77 return "vector select requires selected vectors to have "
78 "the same vector length as select condition";
Owen Anderson55f1c092009-08-13 21:58:54 +000079 } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) {
Chris Lattner88107952008-12-29 00:12:50 +000080 return "select condition must be i1 or <n x i1>";
81 }
Craig Topperc6207612014-04-09 06:08:46 +000082 return nullptr;
Chris Lattner88107952008-12-29 00:12:50 +000083}
84
85
86//===----------------------------------------------------------------------===//
Chris Lattnerafdb3de2005-01-29 00:35:16 +000087// PHINode Class
88//===----------------------------------------------------------------------===//
89
Pete Cooper1d078692015-12-14 20:29:16 +000090void PHINode::anchor() {}
91
Chris Lattnerafdb3de2005-01-29 00:35:16 +000092PHINode::PHINode(const PHINode &PN)
Pete Cooper3fc30402015-06-10 22:38:46 +000093 : Instruction(PN.getType(), Instruction::PHI, nullptr, PN.getNumOperands()),
94 ReservedSpace(PN.getNumOperands()) {
95 allocHungoffUses(PN.getNumOperands());
Jay Foad61ea0e42011-06-23 09:09:15 +000096 std::copy(PN.op_begin(), PN.op_end(), op_begin());
97 std::copy(PN.block_begin(), PN.block_end(), block_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +000098 SubclassOptionalData = PN.SubclassOptionalData;
Chris Lattnerafdb3de2005-01-29 00:35:16 +000099}
100
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000101// removeIncomingValue - Remove an incoming value. This is useful if a
102// predecessor basic block is deleted.
103Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
Jay Foad61ea0e42011-06-23 09:09:15 +0000104 Value *Removed = getIncomingValue(Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000105
106 // Move everything after this operand down.
107 //
108 // FIXME: we could just swap with the end of the list, then erase. However,
Jay Foad61ea0e42011-06-23 09:09:15 +0000109 // clients might not expect this to happen. The code as it is thrashes the
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000110 // use/def lists, which is kinda lame.
Jay Foad61ea0e42011-06-23 09:09:15 +0000111 std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx);
112 std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000113
114 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +0000115 Op<-1>().set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +0000116 setNumHungOffUseOperands(getNumOperands() - 1);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000117
118 // If the PHI node is dead, because it has zero entries, nuke it now.
Jay Foad61ea0e42011-06-23 09:09:15 +0000119 if (getNumOperands() == 0 && DeletePHIIfEmpty) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000120 // If anyone is using this PHI, make them use a dummy value instead...
Owen Andersonb292b8c2009-07-30 23:03:37 +0000121 replaceAllUsesWith(UndefValue::get(getType()));
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000122 eraseFromParent();
123 }
124 return Removed;
125}
126
Jay Foade98f29d2011-04-01 08:00:58 +0000127/// growOperands - grow operands - This grows the operand list in response
128/// to a push_back style of operation. This grows the number of ops by 1.5
129/// times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000130///
Jay Foade98f29d2011-04-01 08:00:58 +0000131void PHINode::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +0000132 unsigned e = getNumOperands();
Jay Foad61ea0e42011-06-23 09:09:15 +0000133 unsigned NumOps = e + e / 2;
134 if (NumOps < 2) NumOps = 2; // 2 op PHI nodes are VERY common.
135
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000136 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000137 growHungoffUses(ReservedSpace, /* IsPhi */ true);
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000138}
139
Nate Begemanb3923212005-08-04 23:24:19 +0000140/// hasConstantValue - If the specified PHI node always merges together the same
141/// value, return the value, otherwise return null.
Duncan Sands7412f6e2010-11-17 04:30:22 +0000142Value *PHINode::hasConstantValue() const {
143 // Exploit the fact that phi nodes always have at least one entry.
144 Value *ConstantValue = getIncomingValue(0);
145 for (unsigned i = 1, e = getNumIncomingValues(); i != e; ++i)
Nuno Lopes90c76df2012-07-03 17:10:28 +0000146 if (getIncomingValue(i) != ConstantValue && getIncomingValue(i) != this) {
147 if (ConstantValue != this)
Craig Topperc6207612014-04-09 06:08:46 +0000148 return nullptr; // Incoming values not all the same.
Nuno Lopes90c76df2012-07-03 17:10:28 +0000149 // The case where the first value is this PHI.
150 ConstantValue = getIncomingValue(i);
151 }
Nuno Lopes0d44a502012-07-03 21:15:40 +0000152 if (ConstantValue == this)
153 return UndefValue::get(getType());
Duncan Sands7412f6e2010-11-17 04:30:22 +0000154 return ConstantValue;
Nate Begemanb3923212005-08-04 23:24:19 +0000155}
156
Bill Wendlingfae14752011-08-12 20:24:12 +0000157//===----------------------------------------------------------------------===//
158// LandingPadInst Implementation
159//===----------------------------------------------------------------------===//
160
David Majnemer7fddecc2015-06-17 20:52:32 +0000161LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
162 const Twine &NameStr, Instruction *InsertBefore)
163 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertBefore) {
164 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000165}
166
David Majnemer7fddecc2015-06-17 20:52:32 +0000167LandingPadInst::LandingPadInst(Type *RetTy, unsigned NumReservedValues,
168 const Twine &NameStr, BasicBlock *InsertAtEnd)
169 : Instruction(RetTy, Instruction::LandingPad, nullptr, 0, InsertAtEnd) {
170 init(NumReservedValues, NameStr);
Bill Wendlingfae14752011-08-12 20:24:12 +0000171}
172
173LandingPadInst::LandingPadInst(const LandingPadInst &LP)
Pete Cooper3fc30402015-06-10 22:38:46 +0000174 : Instruction(LP.getType(), Instruction::LandingPad, nullptr,
175 LP.getNumOperands()),
176 ReservedSpace(LP.getNumOperands()) {
177 allocHungoffUses(LP.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +0000178 Use *OL = getOperandList();
179 const Use *InOL = LP.getOperandList();
Bill Wendlingfae14752011-08-12 20:24:12 +0000180 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
181 OL[I] = InOL[I];
182
183 setCleanup(LP.isCleanup());
184}
185
David Majnemer7fddecc2015-06-17 20:52:32 +0000186LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000187 const Twine &NameStr,
188 Instruction *InsertBefore) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000189 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertBefore);
Bill Wendlingfae14752011-08-12 20:24:12 +0000190}
191
David Majnemer7fddecc2015-06-17 20:52:32 +0000192LandingPadInst *LandingPadInst::Create(Type *RetTy, unsigned NumReservedClauses,
Bill Wendlingfae14752011-08-12 20:24:12 +0000193 const Twine &NameStr,
194 BasicBlock *InsertAtEnd) {
David Majnemer7fddecc2015-06-17 20:52:32 +0000195 return new LandingPadInst(RetTy, NumReservedClauses, NameStr, InsertAtEnd);
Bill Wendlingfae14752011-08-12 20:24:12 +0000196}
197
David Majnemer7fddecc2015-06-17 20:52:32 +0000198void LandingPadInst::init(unsigned NumReservedValues, const Twine &NameStr) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000199 ReservedSpace = NumReservedValues;
David Majnemer7fddecc2015-06-17 20:52:32 +0000200 setNumHungOffUseOperands(0);
Pete Cooper3fc30402015-06-10 22:38:46 +0000201 allocHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000202 setName(NameStr);
203 setCleanup(false);
204}
205
206/// growOperands - grow operands - This grows the operand list in response to a
207/// push_back style of operation. This grows the number of ops by 2 times.
208void LandingPadInst::growOperands(unsigned Size) {
209 unsigned e = getNumOperands();
210 if (ReservedSpace >= e + Size) return;
David Majnemer7fddecc2015-06-17 20:52:32 +0000211 ReservedSpace = (std::max(e, 1U) + Size / 2) * 2;
Pete Cooper93f9ff52015-06-10 22:38:41 +0000212 growHungoffUses(ReservedSpace);
Bill Wendlingfae14752011-08-12 20:24:12 +0000213}
214
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +0000215void LandingPadInst::addClause(Constant *Val) {
Bill Wendlingfae14752011-08-12 20:24:12 +0000216 unsigned OpNo = getNumOperands();
217 growOperands(1);
218 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +0000219 setNumHungOffUseOperands(getNumOperands() + 1);
Pete Cooper74510a42015-06-12 17:48:05 +0000220 getOperandList()[OpNo] = Val;
Bill Wendlingfae14752011-08-12 20:24:12 +0000221}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000222
223//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000224// CallInst Implementation
225//===----------------------------------------------------------------------===//
226
Gordon Henriksen14a55692007-12-10 02:14:30 +0000227CallInst::~CallInst() {
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000228}
229
David Blaikie348de692015-04-23 21:36:23 +0000230void CallInst::init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000231 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000232 this->FTy = FTy;
Sanjoy Das9303c242015-09-24 19:14:18 +0000233 assert(getNumOperands() == Args.size() + CountBundleInputs(Bundles) + 1 &&
234 "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000235 Op<-1>() = Func;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000236
Jay Foad5bd375a2011-07-15 08:37:34 +0000237#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000238 assert((Args.size() == FTy->getNumParams() ||
239 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000240 "Calling a function with bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000241
242 for (unsigned i = 0; i != Args.size(); ++i)
Chris Lattner667a0562006-05-03 00:48:22 +0000243 assert((i >= FTy->getNumParams() ||
Jay Foad5bd375a2011-07-15 08:37:34 +0000244 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000245 "Calling a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000246#endif
247
248 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000249
250 auto It = populateBundleOperandInfos(Bundles, Args.size());
251 (void)It;
252 assert(It + 1 == op_end() && "Should add up!");
253
Jay Foad5bd375a2011-07-15 08:37:34 +0000254 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000255}
256
Jay Foad5bd375a2011-07-15 08:37:34 +0000257void CallInst::init(Value *Func, const Twine &NameStr) {
David Blaikie348de692015-04-23 21:36:23 +0000258 FTy =
259 cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
Pete Cooperb4eede22015-06-12 17:48:10 +0000260 assert(getNumOperands() == 1 && "NumOperands not set up?");
Gabor Greif6d673952010-07-16 09:38:02 +0000261 Op<-1>() = Func;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000262
Chris Lattnerf14c76c2007-02-01 04:59:37 +0000263 assert(FTy->getNumParams() == 0 && "Calling a function with bad signature");
Jay Foad5bd375a2011-07-15 08:37:34 +0000264
265 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000266}
267
Daniel Dunbar4975db62009-07-25 04:41:11 +0000268CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000269 Instruction *InsertBefore)
270 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
271 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000272 Instruction::Call,
273 OperandTraits<CallInst>::op_end(this) - 1,
274 1, InsertBefore) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000275 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000276}
277
Daniel Dunbar4975db62009-07-25 04:41:11 +0000278CallInst::CallInst(Value *Func, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000279 BasicBlock *InsertAtEnd)
280 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
281 ->getElementType())->getReturnType(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000282 Instruction::Call,
283 OperandTraits<CallInst>::op_end(this) - 1,
284 1, InsertAtEnd) {
Jay Foad5bd375a2011-07-15 08:37:34 +0000285 init(Func, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000286}
287
Misha Brukmanb1c93172005-04-21 23:48:37 +0000288CallInst::CallInst(const CallInst &CI)
David Blaikie348de692015-04-23 21:36:23 +0000289 : Instruction(CI.getType(), Instruction::Call,
290 OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(),
291 CI.getNumOperands()),
292 AttributeList(CI.AttributeList), FTy(CI.FTy) {
Reid Kleckner118e1bf2014-05-06 20:08:20 +0000293 setTailCallKind(CI.getTailCallKind());
Chris Lattnerb9c86512009-12-29 02:14:09 +0000294 setCallingConv(CI.getCallingConv());
Sanjoy Das9303c242015-09-24 19:14:18 +0000295
Jay Foad5bd375a2011-07-15 08:37:34 +0000296 std::copy(CI.op_begin(), CI.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000297 std::copy(CI.bundle_op_info_begin(), CI.bundle_op_info_end(),
298 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000299 SubclassOptionalData = CI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000300}
301
Sanjoy Das2d161452015-11-18 06:23:38 +0000302CallInst *CallInst::Create(CallInst *CI, ArrayRef<OperandBundleDef> OpB,
303 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000304 std::vector<Value *> Args(CI->arg_begin(), CI->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000305
306 auto *NewCI = CallInst::Create(CI->getCalledValue(), Args, OpB, CI->getName(),
307 InsertPt);
308 NewCI->setTailCallKind(CI->getTailCallKind());
309 NewCI->setCallingConv(CI->getCallingConv());
310 NewCI->SubclassOptionalData = CI->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000311 NewCI->setAttributes(CI->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000312 return NewCI;
313}
314
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000315void CallInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000316 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000317 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000318 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000319}
320
Akira Hatanaka5c40eeab2015-07-02 22:08:48 +0000321void CallInst::addAttribute(unsigned i, StringRef Kind, StringRef Value) {
322 AttributeSet PAL = getAttributes();
323 PAL = PAL.addAttribute(getContext(), i, Kind, Value);
324 setAttributes(PAL);
325}
326
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000327void CallInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000328 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000329 AttrBuilder B(attr);
330 LLVMContext &Context = getContext();
331 PAL = PAL.removeAttributes(Context, i,
332 AttributeSet::get(Context, i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000333 setAttributes(PAL);
Duncan Sands78c88722008-07-08 08:38:44 +0000334}
335
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000336void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
337 AttributeSet PAL = getAttributes();
338 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
339 setAttributes(PAL);
340}
341
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000342void CallInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
343 AttributeSet PAL = getAttributes();
344 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
345 setAttributes(PAL);
346}
347
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000348bool CallInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000349 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
350
Bill Wendling749a43d2012-12-30 13:50:49 +0000351 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000352 return true;
353 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000354 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000355 return false;
356}
357
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000358bool CallInst::dataOperandHasImpliedAttr(unsigned i,
359 Attribute::AttrKind A) const {
360
Sanjoy Das776e4a72015-11-05 01:53:26 +0000361 // There are getNumOperands() - 1 data operands. The last operand is the
362 // callee.
363 assert(i < getNumOperands() && "Data operand index out of bounds!");
364
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000365 // The attribute A can either be directly specified, if the operand in
366 // question is a call argument; or be indirectly implied by the kind of its
367 // containing operand bundle, if the operand is a bundle operand.
368
369 if (i < (getNumArgOperands() + 1))
370 return paramHasAttr(i, A);
371
372 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
373 "Must be either a call argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000374 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000375}
376
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000377/// IsConstantOne - Return true only if val is constant int 1
378static bool IsConstantOne(Value *val) {
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000379 assert(val && "IsConstantOne does not work with nullptr val");
Matt Arsenault69417852014-09-15 17:56:51 +0000380 const ConstantInt *CVal = dyn_cast<ConstantInt>(val);
381 return CVal && CVal->isOne();
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000382}
383
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000384static Instruction *createMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000385 BasicBlock *InsertAtEnd, Type *IntPtrTy,
386 Type *AllocTy, Value *AllocSize,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000387 Value *ArraySize, Function *MallocF,
388 const Twine &Name) {
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000389 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
Victor Hernandez788eaab2009-09-18 19:20:02 +0000390 "createMalloc needs either InsertBefore or InsertAtEnd");
391
392 // malloc(type) becomes:
393 // bitcast (i8* malloc(typeSize)) to type*
394 // malloc(type, arraySize) becomes:
395 // bitcast (i8 *malloc(typeSize*arraySize)) to type*
Victor Hernandezf3db9152009-11-07 00:16:28 +0000396 if (!ArraySize)
397 ArraySize = ConstantInt::get(IntPtrTy, 1);
398 else if (ArraySize->getType() != IntPtrTy) {
399 if (InsertBefore)
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000400 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
401 "", InsertBefore);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000402 else
Victor Hernandeze04ed0c2009-11-07 00:36:50 +0000403 ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false,
404 "", InsertAtEnd);
Victor Hernandezf3db9152009-11-07 00:16:28 +0000405 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000406
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000407 if (!IsConstantOne(ArraySize)) {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000408 if (IsConstantOne(AllocSize)) {
409 AllocSize = ArraySize; // Operand * 1 = Operand
410 } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) {
411 Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy,
412 false /*ZExt*/);
413 // Malloc arg is constant product of type size and array size
414 AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize));
415 } else {
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000416 // Multiply type size by the array size...
417 if (InsertBefore)
Victor Hernandez788eaab2009-09-18 19:20:02 +0000418 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
419 "mallocsize", InsertBefore);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000420 else
Victor Hernandez788eaab2009-09-18 19:20:02 +0000421 AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize,
422 "mallocsize", InsertAtEnd);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000423 }
Benjamin Kramer4bf4e862009-09-10 11:31:39 +0000424 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000425
Victor Hernandez788eaab2009-09-18 19:20:02 +0000426 assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size");
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000427 // Create the call to Malloc.
428 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
429 Module* M = BB->getParent()->getParent();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000430 Type *BPTy = Type::getInt8PtrTy(BB->getContext());
Victor Hernandezbb336a12009-11-10 19:53:28 +0000431 Value *MallocFunc = MallocF;
432 if (!MallocFunc)
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000433 // prototype malloc as "void *malloc(size_t)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000434 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000435 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
Craig Topperc6207612014-04-09 06:08:46 +0000436 CallInst *MCall = nullptr;
437 Instruction *Result = nullptr;
Victor Hernandez788eaab2009-09-18 19:20:02 +0000438 if (InsertBefore) {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000439 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000440 Result = MCall;
441 if (Result->getType() != AllocPtrType)
442 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000443 Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore);
Victor Hernandez788eaab2009-09-18 19:20:02 +0000444 } else {
Victor Hernandezbb336a12009-11-10 19:53:28 +0000445 MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall");
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000446 Result = MCall;
447 if (Result->getType() != AllocPtrType) {
448 InsertAtEnd->getInstList().push_back(MCall);
449 // Create a cast instruction to convert to the right type...
Victor Hernandezf3db9152009-11-07 00:16:28 +0000450 Result = new BitCastInst(MCall, AllocPtrType, Name);
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000451 }
Victor Hernandez788eaab2009-09-18 19:20:02 +0000452 }
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000453 MCall->setTailCall();
Victor Hernandezbb336a12009-11-10 19:53:28 +0000454 if (Function *F = dyn_cast<Function>(MallocFunc)) {
455 MCall->setCallingConv(F->getCallingConv());
456 if (!F->doesNotAlias(0)) F->setDoesNotAlias(0);
457 }
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000458 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
Victor Hernandez788eaab2009-09-18 19:20:02 +0000459
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000460 return Result;
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000461}
462
463/// CreateMalloc - Generate the IR for a call to malloc:
464/// 1. Compute the malloc call's argument as the specified type's size,
465/// possibly multiplied by the array size if the array size is not
466/// constant 1.
467/// 2. Call malloc with that argument.
468/// 3. Bitcast the result of the malloc call to the specified type.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000469Instruction *CallInst::CreateMalloc(Instruction *InsertBefore,
Chris Lattner229907c2011-07-18 04:54:35 +0000470 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000471 Value *AllocSize, Value *ArraySize,
Duncan Sands41b4a6b2010-07-12 08:16:59 +0000472 Function * MallocF,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000473 const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000474 return createMalloc(InsertBefore, nullptr, IntPtrTy, AllocTy, AllocSize,
Chris Lattner601e390a2010-07-12 00:57:28 +0000475 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000476}
477
478/// CreateMalloc - Generate the IR for a call to malloc:
479/// 1. Compute the malloc call's argument as the specified type's size,
480/// possibly multiplied by the array size if the array size is not
481/// constant 1.
482/// 2. Call malloc with that argument.
483/// 3. Bitcast the result of the malloc call to the specified type.
484/// Note: This function does not add the bitcast to the basic block, that is the
485/// responsibility of the caller.
Nick Lewyckybb1410e2009-10-17 23:52:26 +0000486Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd,
Chris Lattner229907c2011-07-18 04:54:35 +0000487 Type *IntPtrTy, Type *AllocTy,
Victor Hernandezf3db9152009-11-07 00:16:28 +0000488 Value *AllocSize, Value *ArraySize,
489 Function *MallocF, const Twine &Name) {
Craig Topperc6207612014-04-09 06:08:46 +0000490 return createMalloc(nullptr, InsertAtEnd, IntPtrTy, AllocTy, AllocSize,
Victor Hernandezc7d6a832009-10-17 00:00:19 +0000491 ArraySize, MallocF, Name);
Evan Cheng1d9d4bd2009-09-10 04:36:43 +0000492}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000493
Victor Hernandeze2971492009-10-24 04:23:03 +0000494static Instruction* createFree(Value* Source, Instruction *InsertBefore,
495 BasicBlock *InsertAtEnd) {
496 assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) &&
497 "createFree needs either InsertBefore or InsertAtEnd");
Duncan Sands19d0b472010-02-16 11:11:14 +0000498 assert(Source->getType()->isPointerTy() &&
Victor Hernandeze2971492009-10-24 04:23:03 +0000499 "Can not free something of nonpointer type!");
500
501 BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd;
502 Module* M = BB->getParent()->getParent();
503
Chris Lattner229907c2011-07-18 04:54:35 +0000504 Type *VoidTy = Type::getVoidTy(M->getContext());
505 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
Victor Hernandeze2971492009-10-24 04:23:03 +0000506 // prototype free as "void free(void*)"
Reid Kleckner971c3ea2014-11-13 22:55:19 +0000507 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
Craig Topperc6207612014-04-09 06:08:46 +0000508 CallInst* Result = nullptr;
Victor Hernandeze2971492009-10-24 04:23:03 +0000509 Value *PtrCast = Source;
510 if (InsertBefore) {
511 if (Source->getType() != IntPtrTy)
512 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore);
513 Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore);
514 } else {
515 if (Source->getType() != IntPtrTy)
516 PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd);
517 Result = CallInst::Create(FreeFunc, PtrCast, "");
518 }
519 Result->setTailCall();
Chris Lattner2156c222009-11-09 07:12:01 +0000520 if (Function *F = dyn_cast<Function>(FreeFunc))
521 Result->setCallingConv(F->getCallingConv());
Victor Hernandeze2971492009-10-24 04:23:03 +0000522
523 return Result;
524}
525
526/// CreateFree - Generate the IR for a call to the builtin free function.
Chris Lattner601e390a2010-07-12 00:57:28 +0000527Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) {
Craig Topperc6207612014-04-09 06:08:46 +0000528 return createFree(Source, InsertBefore, nullptr);
Victor Hernandeze2971492009-10-24 04:23:03 +0000529}
530
531/// CreateFree - Generate the IR for a call to the builtin free function.
532/// Note: This function does not add the call to the basic block, that is the
533/// responsibility of the caller.
534Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) {
Craig Topperc6207612014-04-09 06:08:46 +0000535 Instruction* FreeCall = createFree(Source, nullptr, InsertAtEnd);
Victor Hernandeze2971492009-10-24 04:23:03 +0000536 assert(FreeCall && "CreateFree did not create a CallInst");
537 return FreeCall;
538}
539
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000540//===----------------------------------------------------------------------===//
541// InvokeInst Implementation
542//===----------------------------------------------------------------------===//
543
David Blaikie3e807092015-05-13 18:35:26 +0000544void InvokeInst::init(FunctionType *FTy, Value *Fn, BasicBlock *IfNormal,
545 BasicBlock *IfException, ArrayRef<Value *> Args,
Sanjoy Das9303c242015-09-24 19:14:18 +0000546 ArrayRef<OperandBundleDef> Bundles,
David Blaikie3e807092015-05-13 18:35:26 +0000547 const Twine &NameStr) {
548 this->FTy = FTy;
David Blaikie348de692015-04-23 21:36:23 +0000549
Sanjoy Das9303c242015-09-24 19:14:18 +0000550 assert(getNumOperands() == 3 + Args.size() + CountBundleInputs(Bundles) &&
551 "NumOperands not set up?");
Gabor Greifa2fbc0a2010-03-24 13:21:49 +0000552 Op<-3>() = Fn;
553 Op<-2>() = IfNormal;
554 Op<-1>() = IfException;
Jay Foad5bd375a2011-07-15 08:37:34 +0000555
556#ifndef NDEBUG
Jay Foad5bd375a2011-07-15 08:37:34 +0000557 assert(((Args.size() == FTy->getNumParams()) ||
558 (FTy->isVarArg() && Args.size() > FTy->getNumParams())) &&
Gabor Greif668d7002010-03-23 13:45:54 +0000559 "Invoking a function with bad signature");
Misha Brukmanb1c93172005-04-21 23:48:37 +0000560
Jay Foad5bd375a2011-07-15 08:37:34 +0000561 for (unsigned i = 0, e = Args.size(); i != e; i++)
Chris Lattner667a0562006-05-03 00:48:22 +0000562 assert((i >= FTy->getNumParams() ||
Chris Lattnerb5fcc282007-02-13 01:04:01 +0000563 FTy->getParamType(i) == Args[i]->getType()) &&
Chris Lattner667a0562006-05-03 00:48:22 +0000564 "Invoking a function with a bad signature!");
Jay Foad5bd375a2011-07-15 08:37:34 +0000565#endif
566
567 std::copy(Args.begin(), Args.end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000568
569 auto It = populateBundleOperandInfos(Bundles, Args.size());
570 (void)It;
571 assert(It + 3 == op_end() && "Should add up!");
572
Jay Foad5bd375a2011-07-15 08:37:34 +0000573 setName(NameStr);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000574}
575
Misha Brukmanb1c93172005-04-21 23:48:37 +0000576InvokeInst::InvokeInst(const InvokeInst &II)
David Blaikie348de692015-04-23 21:36:23 +0000577 : TerminatorInst(II.getType(), Instruction::Invoke,
578 OperandTraits<InvokeInst>::op_end(this) -
579 II.getNumOperands(),
580 II.getNumOperands()),
581 AttributeList(II.AttributeList), FTy(II.FTy) {
Chris Lattnerb9c86512009-12-29 02:14:09 +0000582 setCallingConv(II.getCallingConv());
Jay Foad5bd375a2011-07-15 08:37:34 +0000583 std::copy(II.op_begin(), II.op_end(), op_begin());
Sanjoy Das9303c242015-09-24 19:14:18 +0000584 std::copy(II.bundle_op_info_begin(), II.bundle_op_info_end(),
585 bundle_op_info_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000586 SubclassOptionalData = II.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000587}
588
Sanjoy Das2d161452015-11-18 06:23:38 +0000589InvokeInst *InvokeInst::Create(InvokeInst *II, ArrayRef<OperandBundleDef> OpB,
590 Instruction *InsertPt) {
Sanjoy Dasccd14562015-12-10 06:39:02 +0000591 std::vector<Value *> Args(II->arg_begin(), II->arg_end());
Sanjoy Das2d161452015-11-18 06:23:38 +0000592
593 auto *NewII = InvokeInst::Create(II->getCalledValue(), II->getNormalDest(),
594 II->getUnwindDest(), Args, OpB,
595 II->getName(), InsertPt);
596 NewII->setCallingConv(II->getCallingConv());
597 NewII->SubclassOptionalData = II->SubclassOptionalData;
Sanjoy Dasb8dced52015-12-09 01:01:28 +0000598 NewII->setAttributes(II->getAttributes());
Sanjoy Das2d161452015-11-18 06:23:38 +0000599 return NewII;
600}
601
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000602BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
603 return getSuccessor(idx);
604}
605unsigned InvokeInst::getNumSuccessorsV() const {
606 return getNumSuccessors();
607}
608void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
609 return setSuccessor(idx, B);
610}
611
Michael Gottesman41748d72013-06-27 00:25:01 +0000612bool InvokeInst::hasFnAttrImpl(Attribute::AttrKind A) const {
Bill Wendling749a43d2012-12-30 13:50:49 +0000613 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
Bill Wendling375eb1f2012-10-09 00:28:54 +0000614 return true;
Sanjoy Das98a341b2015-10-22 03:12:22 +0000615
616 // Operand bundles override attributes on the called function, but don't
617 // override attributes directly present on the invoke instruction.
618 if (isFnAttrDisallowedByOpBundle(A))
619 return false;
620
Bill Wendling375eb1f2012-10-09 00:28:54 +0000621 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000622 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
Bill Wendling375eb1f2012-10-09 00:28:54 +0000623 return false;
624}
625
Bill Wendlingc79e42c2012-12-22 00:37:52 +0000626bool InvokeInst::paramHasAttr(unsigned i, Attribute::AttrKind A) const {
Sanjoy Dasb11b4402015-11-04 20:33:45 +0000627 assert(i < (getNumArgOperands() + 1) && "Param index out of bounds!");
628
Bill Wendling749a43d2012-12-30 13:50:49 +0000629 if (AttributeList.hasAttribute(i, A))
Bill Wendling8baa61d2012-10-03 17:54:26 +0000630 return true;
631 if (const Function *F = getCalledFunction())
Bill Wendling94dcaf82012-12-30 12:45:13 +0000632 return F->getAttributes().hasAttribute(i, A);
Bill Wendlingdaf8e382012-10-04 07:18:12 +0000633 return false;
634}
635
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000636bool InvokeInst::dataOperandHasImpliedAttr(unsigned i,
637 Attribute::AttrKind A) const {
Sanjoy Das776e4a72015-11-05 01:53:26 +0000638 // There are getNumOperands() - 3 data operands. The last three operands are
639 // the callee and the two successor basic blocks.
640 assert(i < (getNumOperands() - 2) && "Data operand index out of bounds!");
641
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000642 // The attribute A can either be directly specified, if the operand in
643 // question is an invoke argument; or be indirectly implied by the kind of its
644 // containing operand bundle, if the operand is a bundle operand.
645
646 if (i < (getNumArgOperands() + 1))
647 return paramHasAttr(i, A);
648
649 assert(hasOperandBundles() && i >= (getBundleOperandsStartIndex() + 1) &&
650 "Must be either an invoke argument or an operand bundle!");
Sanjoy Das18ceafe2015-12-04 20:34:37 +0000651 return bundleOperandHasAttr(i - 1, A);
Sanjoy Dasa4bae3b2015-11-04 21:05:24 +0000652}
653
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000654void InvokeInst::addAttribute(unsigned i, Attribute::AttrKind attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000655 AttributeSet PAL = getAttributes();
Peter Collingbourne1b97a9c2013-03-02 01:20:18 +0000656 PAL = PAL.addAttribute(getContext(), i, attr);
Devang Patel4c758ea2008-09-25 21:00:45 +0000657 setAttributes(PAL);
Eric Christopher901b1a72008-05-16 20:39:43 +0000658}
659
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000660void InvokeInst::removeAttribute(unsigned i, Attribute attr) {
Bill Wendlinge94d8432012-12-07 23:16:57 +0000661 AttributeSet PAL = getAttributes();
Bill Wendling430fa9b2013-01-23 00:45:55 +0000662 AttrBuilder B(attr);
663 PAL = PAL.removeAttributes(getContext(), i,
664 AttributeSet::get(getContext(), i, B));
Devang Patel4c758ea2008-09-25 21:00:45 +0000665 setAttributes(PAL);
Duncan Sandsaa31b922007-12-19 21:13:37 +0000666}
667
Ramkumar Ramachandra8fcb4982015-02-14 19:37:54 +0000668void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
669 AttributeSet PAL = getAttributes();
670 PAL = PAL.addDereferenceableAttr(getContext(), i, Bytes);
671 setAttributes(PAL);
672}
673
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000674void InvokeInst::addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes) {
675 AttributeSet PAL = getAttributes();
676 PAL = PAL.addDereferenceableOrNullAttr(getContext(), i, Bytes);
677 setAttributes(PAL);
678}
679
Bill Wendlingfae14752011-08-12 20:24:12 +0000680LandingPadInst *InvokeInst::getLandingPadInst() const {
681 return cast<LandingPadInst>(getUnwindDest()->getFirstNonPHI());
682}
Duncan Sands5208d1a2007-11-28 17:07:01 +0000683
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000684//===----------------------------------------------------------------------===//
685// ReturnInst Implementation
686//===----------------------------------------------------------------------===//
687
Chris Lattner2195fc42007-02-24 00:55:48 +0000688ReturnInst::ReturnInst(const ReturnInst &RI)
Owen Anderson55f1c092009-08-13 21:58:54 +0000689 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000690 OperandTraits<ReturnInst>::op_end(this) -
691 RI.getNumOperands(),
Gabor Greiff6caff662008-05-10 08:32:32 +0000692 RI.getNumOperands()) {
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000693 if (RI.getNumOperands())
Gabor Greif2d3024d2008-05-26 21:33:52 +0000694 Op<0>() = RI.Op<0>();
Dan Gohmanc8a27f22009-08-25 22:11:20 +0000695 SubclassOptionalData = RI.SubclassOptionalData;
Chris Lattner2195fc42007-02-24 00:55:48 +0000696}
697
Owen Anderson55f1c092009-08-13 21:58:54 +0000698ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore)
699 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000700 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
701 InsertBefore) {
Devang Patelc38eb522008-02-26 18:49:29 +0000702 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000703 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000704}
Owen Anderson55f1c092009-08-13 21:58:54 +0000705ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd)
706 : TerminatorInst(Type::getVoidTy(C), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000707 OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal,
708 InsertAtEnd) {
Devang Patelc38eb522008-02-26 18:49:29 +0000709 if (retVal)
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000710 Op<0>() = retVal;
Chris Lattner2195fc42007-02-24 00:55:48 +0000711}
Owen Anderson55f1c092009-08-13 21:58:54 +0000712ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
713 : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret,
Dan Gohmanfa1211f2008-07-23 00:34:11 +0000714 OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) {
Devang Patel59643e52008-02-23 00:35:18 +0000715}
716
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000717unsigned ReturnInst::getNumSuccessorsV() const {
718 return getNumSuccessors();
719}
720
Devang Patelae682fb2008-02-26 17:56:20 +0000721/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
722/// emit the vtable for the class in this translation unit.
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000723void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000724 llvm_unreachable("ReturnInst has no successors!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000725}
726
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000727BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000728 llvm_unreachable("ReturnInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000729}
730
Devang Patel59643e52008-02-23 00:35:18 +0000731ReturnInst::~ReturnInst() {
Devang Patel59643e52008-02-23 00:35:18 +0000732}
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000733
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +0000734//===----------------------------------------------------------------------===//
Bill Wendlingf891bf82011-07-31 06:30:59 +0000735// ResumeInst Implementation
736//===----------------------------------------------------------------------===//
737
738ResumeInst::ResumeInst(const ResumeInst &RI)
739 : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Resume,
740 OperandTraits<ResumeInst>::op_begin(this), 1) {
741 Op<0>() = RI.Op<0>();
742}
743
744ResumeInst::ResumeInst(Value *Exn, Instruction *InsertBefore)
745 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
746 OperandTraits<ResumeInst>::op_begin(this), 1, InsertBefore) {
747 Op<0>() = Exn;
748}
749
750ResumeInst::ResumeInst(Value *Exn, BasicBlock *InsertAtEnd)
751 : TerminatorInst(Type::getVoidTy(Exn->getContext()), Instruction::Resume,
752 OperandTraits<ResumeInst>::op_begin(this), 1, InsertAtEnd) {
753 Op<0>() = Exn;
754}
755
756unsigned ResumeInst::getNumSuccessorsV() const {
757 return getNumSuccessors();
758}
759
760void ResumeInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
761 llvm_unreachable("ResumeInst has no successors!");
762}
763
764BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
765 llvm_unreachable("ResumeInst has no successors!");
Bill Wendlingf891bf82011-07-31 06:30:59 +0000766}
767
768//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000769// CleanupReturnInst Implementation
770//===----------------------------------------------------------------------===//
771
772CleanupReturnInst::CleanupReturnInst(const CleanupReturnInst &CRI)
773 : TerminatorInst(CRI.getType(), Instruction::CleanupRet,
774 OperandTraits<CleanupReturnInst>::op_end(this) -
775 CRI.getNumOperands(),
776 CRI.getNumOperands()) {
David Majnemereb518bd2015-08-04 08:21:40 +0000777 setInstructionSubclassData(CRI.getSubclassDataFromInstruction());
David Majnemer8a1c45d2015-12-12 05:38:55 +0000778 Op<0>() = CRI.Op<0>();
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000779 if (CRI.hasUnwindDest())
David Majnemer8a1c45d2015-12-12 05:38:55 +0000780 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000781}
782
David Majnemer8a1c45d2015-12-12 05:38:55 +0000783void CleanupReturnInst::init(Value *CleanupPad, BasicBlock *UnwindBB) {
David Majnemer654e1302015-07-31 17:58:14 +0000784 if (UnwindBB)
785 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
David Majnemer654e1302015-07-31 17:58:14 +0000786
David Majnemer8a1c45d2015-12-12 05:38:55 +0000787 Op<0>() = CleanupPad;
David Majnemer654e1302015-07-31 17:58:14 +0000788 if (UnwindBB)
David Majnemer8a1c45d2015-12-12 05:38:55 +0000789 Op<1>() = UnwindBB;
David Majnemer654e1302015-07-31 17:58:14 +0000790}
791
David Majnemer8a1c45d2015-12-12 05:38:55 +0000792CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
793 unsigned Values, Instruction *InsertBefore)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000794 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
795 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000796 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
797 Values, InsertBefore) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000798 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000799}
800
David Majnemer8a1c45d2015-12-12 05:38:55 +0000801CleanupReturnInst::CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB,
802 unsigned Values, BasicBlock *InsertAtEnd)
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000803 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
804 Instruction::CleanupRet,
David Majnemer654e1302015-07-31 17:58:14 +0000805 OperandTraits<CleanupReturnInst>::op_end(this) - Values,
806 Values, InsertAtEnd) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000807 init(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +0000808}
809
810BasicBlock *CleanupReturnInst::getSuccessorV(unsigned Idx) const {
811 assert(Idx == 0);
812 return getUnwindDest();
813}
814unsigned CleanupReturnInst::getNumSuccessorsV() const {
815 return getNumSuccessors();
816}
817void CleanupReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
818 assert(Idx == 0);
819 setUnwindDest(B);
820}
821
822//===----------------------------------------------------------------------===//
David Majnemer654e1302015-07-31 17:58:14 +0000823// CatchReturnInst Implementation
824//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000825void CatchReturnInst::init(Value *CatchPad, BasicBlock *BB) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000826 Op<0>() = CatchPad;
827 Op<1>() = BB;
David Majnemer0bc0eef2015-08-15 02:46:08 +0000828}
David Majnemer654e1302015-07-31 17:58:14 +0000829
830CatchReturnInst::CatchReturnInst(const CatchReturnInst &CRI)
831 : TerminatorInst(Type::getVoidTy(CRI.getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000832 OperandTraits<CatchReturnInst>::op_begin(this), 2) {
833 Op<0>() = CRI.Op<0>();
834 Op<1>() = CRI.Op<1>();
David Majnemer654e1302015-07-31 17:58:14 +0000835}
836
David Majnemer8a1c45d2015-12-12 05:38:55 +0000837CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000838 Instruction *InsertBefore)
David Majnemer654e1302015-07-31 17:58:14 +0000839 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000840 OperandTraits<CatchReturnInst>::op_begin(this), 2,
841 InsertBefore) {
842 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000843}
844
David Majnemer8a1c45d2015-12-12 05:38:55 +0000845CatchReturnInst::CatchReturnInst(Value *CatchPad, BasicBlock *BB,
David Majnemer0bc0eef2015-08-15 02:46:08 +0000846 BasicBlock *InsertAtEnd)
David Majnemer654e1302015-07-31 17:58:14 +0000847 : TerminatorInst(Type::getVoidTy(BB->getContext()), Instruction::CatchRet,
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +0000848 OperandTraits<CatchReturnInst>::op_begin(this), 2,
849 InsertAtEnd) {
850 init(CatchPad, BB);
David Majnemer654e1302015-07-31 17:58:14 +0000851}
852
853BasicBlock *CatchReturnInst::getSuccessorV(unsigned Idx) const {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000854 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000855 return getSuccessor();
856}
857unsigned CatchReturnInst::getNumSuccessorsV() const {
858 return getNumSuccessors();
859}
860void CatchReturnInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
David Majnemerb01aa9f2015-08-23 19:22:31 +0000861 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
David Majnemer654e1302015-07-31 17:58:14 +0000862 setSuccessor(B);
863}
864
865//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000866// CatchSwitchInst Implementation
David Majnemer654e1302015-07-31 17:58:14 +0000867//===----------------------------------------------------------------------===//
David Majnemer8a1c45d2015-12-12 05:38:55 +0000868
869CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
870 unsigned NumReservedValues,
871 const Twine &NameStr,
872 Instruction *InsertBefore)
873 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
874 InsertBefore) {
875 if (UnwindDest)
876 ++NumReservedValues;
877 init(ParentPad, UnwindDest, NumReservedValues + 1);
David Majnemer654e1302015-07-31 17:58:14 +0000878 setName(NameStr);
879}
880
David Majnemer8a1c45d2015-12-12 05:38:55 +0000881CatchSwitchInst::CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
882 unsigned NumReservedValues,
883 const Twine &NameStr, BasicBlock *InsertAtEnd)
884 : TerminatorInst(ParentPad->getType(), Instruction::CatchSwitch, nullptr, 0,
David Majnemer5c73c942015-08-13 22:11:40 +0000885 InsertAtEnd) {
David Majnemer8a1c45d2015-12-12 05:38:55 +0000886 if (UnwindDest)
887 ++NumReservedValues;
888 init(ParentPad, UnwindDest, NumReservedValues + 1);
889 setName(NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000890}
891
David Majnemer8a1c45d2015-12-12 05:38:55 +0000892CatchSwitchInst::CatchSwitchInst(const CatchSwitchInst &CSI)
893 : TerminatorInst(CSI.getType(), Instruction::CatchSwitch, nullptr,
894 CSI.getNumOperands()) {
895 init(CSI.getParentPad(), CSI.getUnwindDest(), CSI.getNumOperands());
896 setNumHungOffUseOperands(ReservedSpace);
897 Use *OL = getOperandList();
898 const Use *InOL = CSI.getOperandList();
899 for (unsigned I = 1, E = ReservedSpace; I != E; ++I)
900 OL[I] = InOL[I];
David Majnemer654e1302015-07-31 17:58:14 +0000901}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000902
903void CatchSwitchInst::init(Value *ParentPad, BasicBlock *UnwindDest,
904 unsigned NumReservedValues) {
905 assert(ParentPad && NumReservedValues);
906
907 ReservedSpace = NumReservedValues;
908 setNumHungOffUseOperands(UnwindDest ? 2 : 1);
909 allocHungoffUses(ReservedSpace);
910
911 Op<0>() = ParentPad;
912 if (UnwindDest) {
913 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
914 setUnwindDest(UnwindDest);
915 }
916}
917
918/// growOperands - grow operands - This grows the operand list in response to a
919/// push_back style of operation. This grows the number of ops by 2 times.
920void CatchSwitchInst::growOperands(unsigned Size) {
921 unsigned NumOperands = getNumOperands();
922 assert(NumOperands >= 1);
923 if (ReservedSpace >= NumOperands + Size)
924 return;
925 ReservedSpace = (NumOperands + Size / 2) * 2;
926 growHungoffUses(ReservedSpace);
927}
928
929void CatchSwitchInst::addHandler(BasicBlock *Handler) {
930 unsigned OpNo = getNumOperands();
931 growOperands(1);
932 assert(OpNo < ReservedSpace && "Growing didn't work!");
933 setNumHungOffUseOperands(getNumOperands() + 1);
934 getOperandList()[OpNo] = Handler;
935}
936
937BasicBlock *CatchSwitchInst::getSuccessorV(unsigned idx) const {
938 return getSuccessor(idx);
939}
940unsigned CatchSwitchInst::getNumSuccessorsV() const {
David Majnemer654e1302015-07-31 17:58:14 +0000941 return getNumSuccessors();
942}
David Majnemer8a1c45d2015-12-12 05:38:55 +0000943void CatchSwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
944 setSuccessor(idx, B);
945}
946
947//===----------------------------------------------------------------------===//
948// FuncletPadInst Implementation
949//===----------------------------------------------------------------------===//
950void FuncletPadInst::init(Value *ParentPad, ArrayRef<Value *> Args,
951 const Twine &NameStr) {
952 assert(getNumOperands() == 1 + Args.size() && "NumOperands not set up?");
953 std::copy(Args.begin(), Args.end(), op_begin());
954 setParentPad(ParentPad);
955 setName(NameStr);
956}
957
958FuncletPadInst::FuncletPadInst(const FuncletPadInst &FPI)
959 : Instruction(FPI.getType(), FPI.getOpcode(),
960 OperandTraits<FuncletPadInst>::op_end(this) -
961 FPI.getNumOperands(),
962 FPI.getNumOperands()) {
963 std::copy(FPI.op_begin(), FPI.op_end(), op_begin());
964 setParentPad(FPI.getParentPad());
965}
966
967FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
968 ArrayRef<Value *> Args, unsigned Values,
969 const Twine &NameStr, Instruction *InsertBefore)
970 : Instruction(ParentPad->getType(), Op,
971 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
972 InsertBefore) {
973 init(ParentPad, Args, NameStr);
974}
975
976FuncletPadInst::FuncletPadInst(Instruction::FuncletPadOps Op, Value *ParentPad,
977 ArrayRef<Value *> Args, unsigned Values,
978 const Twine &NameStr, BasicBlock *InsertAtEnd)
979 : Instruction(ParentPad->getType(), Op,
980 OperandTraits<FuncletPadInst>::op_end(this) - Values, Values,
981 InsertAtEnd) {
982 init(ParentPad, Args, NameStr);
David Majnemer654e1302015-07-31 17:58:14 +0000983}
984
985//===----------------------------------------------------------------------===//
Chris Lattner5e0b9f22004-10-16 18:08:06 +0000986// UnreachableInst Implementation
987//===----------------------------------------------------------------------===//
988
Owen Anderson55f1c092009-08-13 21:58:54 +0000989UnreachableInst::UnreachableInst(LLVMContext &Context,
990 Instruction *InsertBefore)
991 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000992 nullptr, 0, InsertBefore) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000993}
Owen Anderson55f1c092009-08-13 21:58:54 +0000994UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd)
995 : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable,
Craig Topperc6207612014-04-09 06:08:46 +0000996 nullptr, 0, InsertAtEnd) {
Chris Lattner2195fc42007-02-24 00:55:48 +0000997}
998
Chris Lattnerafdb3de2005-01-29 00:35:16 +0000999unsigned UnreachableInst::getNumSuccessorsV() const {
1000 return getNumSuccessors();
1001}
1002
1003void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001004 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001005}
1006
1007BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
Bill Wendling0aef16a2012-02-06 21:44:22 +00001008 llvm_unreachable("UnreachableInst has no successors!");
Chris Lattner5e0b9f22004-10-16 18:08:06 +00001009}
1010
1011//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001012// BranchInst Implementation
1013//===----------------------------------------------------------------------===//
1014
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001015void BranchInst::AssertOK() {
1016 if (isConditional())
Duncan Sands9dff9be2010-02-15 16:12:20 +00001017 assert(getCondition()->getType()->isIntegerTy(1) &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001018 "May only branch on boolean predicates!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001019}
1020
Chris Lattner2195fc42007-02-24 00:55:48 +00001021BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001022 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001023 OperandTraits<BranchInst>::op_end(this) - 1,
1024 1, InsertBefore) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001025 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001026 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001027}
1028BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1029 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001030 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001031 OperandTraits<BranchInst>::op_end(this) - 3,
1032 3, InsertBefore) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001033 Op<-1>() = IfTrue;
1034 Op<-2>() = IfFalse;
1035 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001036#ifndef NDEBUG
1037 AssertOK();
1038#endif
1039}
1040
1041BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001042 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001043 OperandTraits<BranchInst>::op_end(this) - 1,
1044 1, InsertAtEnd) {
Craig Topper2617dcc2014-04-15 06:32:26 +00001045 assert(IfTrue && "Branch destination may not be null!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001046 Op<-1>() = IfTrue;
Chris Lattner2195fc42007-02-24 00:55:48 +00001047}
1048
1049BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1050 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00001051 : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001052 OperandTraits<BranchInst>::op_end(this) - 3,
1053 3, InsertAtEnd) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001054 Op<-1>() = IfTrue;
1055 Op<-2>() = IfFalse;
1056 Op<-3>() = Cond;
Chris Lattner2195fc42007-02-24 00:55:48 +00001057#ifndef NDEBUG
1058 AssertOK();
1059#endif
1060}
1061
1062
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001063BranchInst::BranchInst(const BranchInst &BI) :
Owen Anderson55f1c092009-08-13 21:58:54 +00001064 TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br,
Gabor Greiff6caff662008-05-10 08:32:32 +00001065 OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(),
1066 BI.getNumOperands()) {
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001067 Op<-1>() = BI.Op<-1>();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001068 if (BI.getNumOperands() != 1) {
1069 assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
Gabor Greifc91aa9b2009-03-12 18:34:49 +00001070 Op<-3>() = BI.Op<-3>();
1071 Op<-2>() = BI.Op<-2>();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001072 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001073 SubclassOptionalData = BI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001074}
1075
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001076void BranchInst::swapSuccessors() {
1077 assert(isConditional() &&
1078 "Cannot swap successors of an unconditional branch");
1079 Op<-1>().swap(Op<-2>());
1080
1081 // Update profile metadata if present and it matches our structural
1082 // expectations.
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001083 MDNode *ProfileData = getMetadata(LLVMContext::MD_prof);
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001084 if (!ProfileData || ProfileData->getNumOperands() != 3)
1085 return;
1086
1087 // The first operand is the name. Fetch them backwards and build a new one.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001088 Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
1089 ProfileData->getOperand(1)};
Chandler Carruth3e8aa652011-10-17 01:11:57 +00001090 setMetadata(LLVMContext::MD_prof,
1091 MDNode::get(ProfileData->getContext(), Ops));
1092}
1093
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001094BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
1095 return getSuccessor(idx);
1096}
1097unsigned BranchInst::getNumSuccessorsV() const {
1098 return getNumSuccessors();
1099}
1100void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
1101 setSuccessor(idx, B);
1102}
1103
1104
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001105//===----------------------------------------------------------------------===//
Victor Hernandez8acf2952009-10-23 21:09:37 +00001106// AllocaInst Implementation
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001107//===----------------------------------------------------------------------===//
1108
Owen Andersonb6b25302009-07-14 23:09:55 +00001109static Value *getAISize(LLVMContext &Context, Value *Amt) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001110 if (!Amt)
Owen Anderson55f1c092009-08-13 21:58:54 +00001111 Amt = ConstantInt::get(Type::getInt32Ty(Context), 1);
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001112 else {
1113 assert(!isa<BasicBlock>(Amt) &&
Chris Lattner9b6ec772007-10-18 16:10:48 +00001114 "Passed basic block into allocation size parameter! Use other ctor");
Dan Gohman2140a742010-05-28 01:14:11 +00001115 assert(Amt->getType()->isIntegerTy() &&
1116 "Allocation array size is not an integer!");
Chris Lattnerbb7ff662006-05-10 04:32:43 +00001117 }
Misha Brukmanb1c93172005-04-21 23:48:37 +00001118 return Amt;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001119}
1120
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001121AllocaInst::AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore)
1122 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001123
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001124AllocaInst::AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd)
1125 : AllocaInst(Ty, /*ArraySize=*/nullptr, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001126
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001127AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001128 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001129 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertBefore) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001130
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001131AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, const Twine &Name,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001132 BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001133 : AllocaInst(Ty, ArraySize, /*Align=*/0, Name, InsertAtEnd) {}
Victor Hernandez8acf2952009-10-23 21:09:37 +00001134
Chris Lattner229907c2011-07-18 04:54:35 +00001135AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001136 const Twine &Name, Instruction *InsertBefore)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001137 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1138 getAISize(Ty->getContext(), ArraySize), InsertBefore),
1139 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001140 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001141 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001142 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001143}
1144
Chris Lattner229907c2011-07-18 04:54:35 +00001145AllocaInst::AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
Victor Hernandez8acf2952009-10-23 21:09:37 +00001146 const Twine &Name, BasicBlock *InsertAtEnd)
David Blaikiebf0a42a2015-04-29 23:00:35 +00001147 : UnaryInstruction(PointerType::getUnqual(Ty), Alloca,
1148 getAISize(Ty->getContext(), ArraySize), InsertAtEnd),
1149 AllocatedType(Ty) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001150 setAlignment(Align);
Benjamin Kramerccce8ba2010-01-05 13:12:22 +00001151 assert(!Ty->isVoidTy() && "Cannot allocate void!");
Chris Lattner0f048162007-02-13 07:54:42 +00001152 setName(Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001153}
1154
Gordon Henriksen14a55692007-12-10 02:14:30 +00001155// Out of line virtual method, so the vtable, etc has a home.
Victor Hernandez8acf2952009-10-23 21:09:37 +00001156AllocaInst::~AllocaInst() {
Gordon Henriksen14a55692007-12-10 02:14:30 +00001157}
1158
Victor Hernandez8acf2952009-10-23 21:09:37 +00001159void AllocaInst::setAlignment(unsigned Align) {
Dan Gohmanaa583d72008-03-24 16:55:58 +00001160 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001161 assert(Align <= MaximumAlignment &&
1162 "Alignment is greater than MaximumAlignment!");
Reid Kleckner436c42e2014-01-17 23:58:17 +00001163 setInstructionSubclassData((getSubclassDataFromInstruction() & ~31) |
1164 (Log2_32(Align) + 1));
Dan Gohmanaa583d72008-03-24 16:55:58 +00001165 assert(getAlignment() == Align && "Alignment representation error!");
1166}
1167
Victor Hernandez8acf2952009-10-23 21:09:37 +00001168bool AllocaInst::isArrayAllocation() const {
Reid Spencera9e6e312007-03-01 20:27:41 +00001169 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
Dan Gohman9a1b8592010-09-27 15:15:44 +00001170 return !CI->isOne();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001171 return true;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001172}
1173
Chris Lattner8b291e62008-11-26 02:54:17 +00001174/// isStaticAlloca - Return true if this alloca is in the entry block of the
1175/// function and is a constant size. If so, the code generator will fold it
1176/// into the prolog/epilog code, so it is basically free.
1177bool AllocaInst::isStaticAlloca() const {
1178 // Must be constant size.
1179 if (!isa<ConstantInt>(getArraySize())) return false;
1180
1181 // Must be in the entry block.
1182 const BasicBlock *Parent = getParent();
Reid Kleckner436c42e2014-01-17 23:58:17 +00001183 return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca();
Chris Lattner8b291e62008-11-26 02:54:17 +00001184}
1185
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001186//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001187// LoadInst Implementation
1188//===----------------------------------------------------------------------===//
1189
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001190void LoadInst::AssertOK() {
Duncan Sands19d0b472010-02-16 11:11:14 +00001191 assert(getOperand(0)->getType()->isPointerTy() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001192 "Ptr must have pointer type.");
Eli Friedman59b66882011-08-09 23:02:53 +00001193 assert(!(isAtomic() && getAlignment() == 0) &&
1194 "Alignment required for atomic load");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001195}
1196
Daniel Dunbar4975db62009-07-25 04:41:11 +00001197LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001198 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001199
Daniel Dunbar4975db62009-07-25 04:41:11 +00001200LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001201 : LoadInst(Ptr, Name, /*isVolatile=*/false, InsertAE) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001202
David Blaikie0c28fd72015-05-20 21:46:30 +00001203LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001204 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001205 : LoadInst(Ty, Ptr, Name, isVolatile, /*Align=*/0, InsertBef) {}
Eli Friedman59b66882011-08-09 23:02:53 +00001206
1207LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1208 BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001209 : LoadInst(Ptr, Name, isVolatile, /*Align=*/0, InsertAE) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001210
David Blaikieb7a029872015-04-17 19:56:21 +00001211LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Christopher Lamb84485702007-04-22 19:24:39 +00001212 unsigned Align, Instruction *InsertBef)
David Blaikieb7a029872015-04-17 19:56:21 +00001213 : LoadInst(Ty, Ptr, Name, isVolatile, Align, NotAtomic, CrossThread,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001214 InsertBef) {}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001215
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001216LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
Dan Gohman68659282007-07-18 20:51:11 +00001217 unsigned Align, BasicBlock *InsertAE)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001218 : LoadInst(Ptr, Name, isVolatile, Align, NotAtomic, CrossThread, InsertAE) {
Dan Gohman68659282007-07-18 20:51:11 +00001219}
1220
David Blaikie15d9a4c2015-04-06 20:59:48 +00001221LoadInst::LoadInst(Type *Ty, Value *Ptr, const Twine &Name, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001222 unsigned Align, AtomicOrdering Order,
David Blaikie15d9a4c2015-04-06 20:59:48 +00001223 SynchronizationScope SynchScope, Instruction *InsertBef)
1224 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
David Blaikie79009f82015-05-20 20:22:31 +00001225 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Eli Friedman59b66882011-08-09 23:02:53 +00001226 setVolatile(isVolatile);
1227 setAlignment(Align);
1228 setAtomic(Order, SynchScope);
1229 AssertOK();
1230 setName(Name);
1231}
1232
1233LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile,
1234 unsigned Align, AtomicOrdering Order,
1235 SynchronizationScope SynchScope,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001236 BasicBlock *InsertAE)
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001237 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
Chris Lattner2195fc42007-02-24 00:55:48 +00001238 Load, Ptr, InsertAE) {
Chris Lattner0f048162007-02-13 07:54:42 +00001239 setVolatile(isVolatile);
Eli Friedman59b66882011-08-09 23:02:53 +00001240 setAlignment(Align);
1241 setAtomic(Order, SynchScope);
Chris Lattner0f048162007-02-13 07:54:42 +00001242 AssertOK();
1243 setName(Name);
1244}
1245
Daniel Dunbar27096822009-08-11 18:11:15 +00001246LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef)
1247 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1248 Load, Ptr, InsertBef) {
1249 setVolatile(false);
1250 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001251 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001252 AssertOK();
1253 if (Name && Name[0]) setName(Name);
1254}
1255
1256LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE)
1257 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1258 Load, Ptr, InsertAE) {
1259 setVolatile(false);
1260 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001261 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001262 AssertOK();
1263 if (Name && Name[0]) setName(Name);
1264}
1265
David Blaikie0c28fd72015-05-20 21:46:30 +00001266LoadInst::LoadInst(Type *Ty, Value *Ptr, const char *Name, bool isVolatile,
Daniel Dunbar27096822009-08-11 18:11:15 +00001267 Instruction *InsertBef)
David Blaikie0c28fd72015-05-20 21:46:30 +00001268 : UnaryInstruction(Ty, Load, Ptr, InsertBef) {
1269 assert(Ty == cast<PointerType>(Ptr->getType())->getElementType());
Daniel Dunbar27096822009-08-11 18:11:15 +00001270 setVolatile(isVolatile);
1271 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001272 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001273 AssertOK();
1274 if (Name && Name[0]) setName(Name);
1275}
1276
1277LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile,
1278 BasicBlock *InsertAE)
1279 : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
1280 Load, Ptr, InsertAE) {
1281 setVolatile(isVolatile);
1282 setAlignment(0);
Eli Friedman59b66882011-08-09 23:02:53 +00001283 setAtomic(NotAtomic);
Daniel Dunbar27096822009-08-11 18:11:15 +00001284 AssertOK();
1285 if (Name && Name[0]) setName(Name);
1286}
1287
Christopher Lamb84485702007-04-22 19:24:39 +00001288void LoadInst::setAlignment(unsigned Align) {
1289 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001290 assert(Align <= MaximumAlignment &&
1291 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001292 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001293 ((Log2_32(Align)+1)<<1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001294 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001295}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001296
1297//===----------------------------------------------------------------------===//
1298// StoreInst Implementation
1299//===----------------------------------------------------------------------===//
1300
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001301void StoreInst::AssertOK() {
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001302 assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001303 assert(getOperand(1)->getType()->isPointerTy() &&
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001304 "Ptr must have pointer type!");
1305 assert(getOperand(0)->getType() ==
1306 cast<PointerType>(getOperand(1)->getType())->getElementType()
Alkis Evlogimenos079fbde2004-08-06 14:33:37 +00001307 && "Ptr must be a pointer to Val type!");
Eli Friedman59b66882011-08-09 23:02:53 +00001308 assert(!(isAtomic() && getAlignment() == 0) &&
Mark Lacey1d7c97e2013-12-21 00:00:49 +00001309 "Alignment required for atomic store");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001310}
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001311
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001312StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001313 : StoreInst(val, addr, /*isVolatile=*/false, InsertBefore) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001314
1315StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001316 : StoreInst(val, addr, /*isVolatile=*/false, InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001317
Misha Brukmanb1c93172005-04-21 23:48:37 +00001318StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001319 Instruction *InsertBefore)
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001320 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertBefore) {}
Christopher Lamb84485702007-04-22 19:24:39 +00001321
1322StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Benjamin Kramerfc165f12015-03-05 22:05:26 +00001323 BasicBlock *InsertAtEnd)
1324 : StoreInst(val, addr, isVolatile, /*Align=*/0, InsertAtEnd) {}
1325
1326StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1327 Instruction *InsertBefore)
1328 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1329 InsertBefore) {}
1330
1331StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, unsigned Align,
1332 BasicBlock *InsertAtEnd)
1333 : StoreInst(val, addr, isVolatile, Align, NotAtomic, CrossThread,
1334 InsertAtEnd) {}
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001335
Misha Brukmanb1c93172005-04-21 23:48:37 +00001336StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001337 unsigned Align, AtomicOrdering Order,
1338 SynchronizationScope SynchScope,
1339 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00001340 : Instruction(Type::getVoidTy(val->getContext()), Store,
Gabor Greiff6caff662008-05-10 08:32:32 +00001341 OperandTraits<StoreInst>::op_begin(this),
1342 OperandTraits<StoreInst>::operands(this),
Eli Friedman59b66882011-08-09 23:02:53 +00001343 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001344 Op<0>() = val;
1345 Op<1>() = addr;
Dan Gohman68659282007-07-18 20:51:11 +00001346 setVolatile(isVolatile);
1347 setAlignment(Align);
Eli Friedman59b66882011-08-09 23:02:53 +00001348 setAtomic(Order, SynchScope);
Dan Gohman68659282007-07-18 20:51:11 +00001349 AssertOK();
1350}
1351
1352StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
Eli Friedman59b66882011-08-09 23:02:53 +00001353 unsigned Align, AtomicOrdering Order,
1354 SynchronizationScope SynchScope,
1355 BasicBlock *InsertAtEnd)
1356 : Instruction(Type::getVoidTy(val->getContext()), Store,
1357 OperandTraits<StoreInst>::op_begin(this),
1358 OperandTraits<StoreInst>::operands(this),
1359 InsertAtEnd) {
1360 Op<0>() = val;
1361 Op<1>() = addr;
1362 setVolatile(isVolatile);
1363 setAlignment(Align);
1364 setAtomic(Order, SynchScope);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001365 AssertOK();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001366}
1367
Christopher Lamb84485702007-04-22 19:24:39 +00001368void StoreInst::setAlignment(unsigned Align) {
1369 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
Dan Gohmana7e5a242010-07-28 20:12:04 +00001370 assert(Align <= MaximumAlignment &&
1371 "Alignment is greater than MaximumAlignment!");
Eli Friedman59b66882011-08-09 23:02:53 +00001372 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(31 << 1)) |
Chris Lattnerd8eb2cf2009-12-29 02:46:09 +00001373 ((Log2_32(Align)+1) << 1));
Dan Gohmana7e5a242010-07-28 20:12:04 +00001374 assert(getAlignment() == Align && "Alignment representation error!");
Christopher Lamb84485702007-04-22 19:24:39 +00001375}
1376
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001377//===----------------------------------------------------------------------===//
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001378// AtomicCmpXchgInst Implementation
1379//===----------------------------------------------------------------------===//
1380
1381void AtomicCmpXchgInst::Init(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001382 AtomicOrdering SuccessOrdering,
1383 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001384 SynchronizationScope SynchScope) {
1385 Op<0>() = Ptr;
1386 Op<1>() = Cmp;
1387 Op<2>() = NewVal;
Tim Northovere94a5182014-03-11 10:48:52 +00001388 setSuccessOrdering(SuccessOrdering);
1389 setFailureOrdering(FailureOrdering);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001390 setSynchScope(SynchScope);
1391
1392 assert(getOperand(0) && getOperand(1) && getOperand(2) &&
1393 "All operands must be non-null!");
1394 assert(getOperand(0)->getType()->isPointerTy() &&
1395 "Ptr must have pointer type!");
1396 assert(getOperand(1)->getType() ==
1397 cast<PointerType>(getOperand(0)->getType())->getElementType()
1398 && "Ptr must be a pointer to Cmp type!");
1399 assert(getOperand(2)->getType() ==
1400 cast<PointerType>(getOperand(0)->getType())->getElementType()
1401 && "Ptr must be a pointer to NewVal type!");
Tim Northovere94a5182014-03-11 10:48:52 +00001402 assert(SuccessOrdering != NotAtomic &&
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001403 "AtomicCmpXchg instructions must be atomic!");
Tim Northovere94a5182014-03-11 10:48:52 +00001404 assert(FailureOrdering != NotAtomic &&
1405 "AtomicCmpXchg instructions must be atomic!");
1406 assert(SuccessOrdering >= FailureOrdering &&
1407 "AtomicCmpXchg success ordering must be at least as strong as fail");
1408 assert(FailureOrdering != Release && FailureOrdering != AcquireRelease &&
1409 "AtomicCmpXchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001410}
1411
1412AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001413 AtomicOrdering SuccessOrdering,
1414 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001415 SynchronizationScope SynchScope,
1416 Instruction *InsertBefore)
Tim Northover420a2162014-06-13 14:24:07 +00001417 : Instruction(
1418 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1419 nullptr),
1420 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1421 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
Tim Northovere94a5182014-03-11 10:48:52 +00001422 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001423}
1424
1425AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
Tim Northovere94a5182014-03-11 10:48:52 +00001426 AtomicOrdering SuccessOrdering,
1427 AtomicOrdering FailureOrdering,
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001428 SynchronizationScope SynchScope,
1429 BasicBlock *InsertAtEnd)
Tim Northover420a2162014-06-13 14:24:07 +00001430 : Instruction(
1431 StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1432 nullptr),
1433 AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
1434 OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
Tim Northovere94a5182014-03-11 10:48:52 +00001435 Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001436}
Tim Northover420a2162014-06-13 14:24:07 +00001437
Eli Friedmanc9a551e2011-07-28 21:48:00 +00001438//===----------------------------------------------------------------------===//
1439// AtomicRMWInst Implementation
1440//===----------------------------------------------------------------------===//
1441
1442void AtomicRMWInst::Init(BinOp Operation, Value *Ptr, Value *Val,
1443 AtomicOrdering Ordering,
1444 SynchronizationScope SynchScope) {
1445 Op<0>() = Ptr;
1446 Op<1>() = Val;
1447 setOperation(Operation);
1448 setOrdering(Ordering);
1449 setSynchScope(SynchScope);
1450
1451 assert(getOperand(0) && getOperand(1) &&
1452 "All operands must be non-null!");
1453 assert(getOperand(0)->getType()->isPointerTy() &&
1454 "Ptr must have pointer type!");
1455 assert(getOperand(1)->getType() ==
1456 cast<PointerType>(getOperand(0)->getType())->getElementType()
1457 && "Ptr must be a pointer to Val type!");
1458 assert(Ordering != NotAtomic &&
1459 "AtomicRMW instructions must be atomic!");
1460}
1461
1462AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1463 AtomicOrdering Ordering,
1464 SynchronizationScope SynchScope,
1465 Instruction *InsertBefore)
1466 : Instruction(Val->getType(), AtomicRMW,
1467 OperandTraits<AtomicRMWInst>::op_begin(this),
1468 OperandTraits<AtomicRMWInst>::operands(this),
1469 InsertBefore) {
1470 Init(Operation, Ptr, Val, Ordering, SynchScope);
1471}
1472
1473AtomicRMWInst::AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
1474 AtomicOrdering Ordering,
1475 SynchronizationScope SynchScope,
1476 BasicBlock *InsertAtEnd)
1477 : Instruction(Val->getType(), AtomicRMW,
1478 OperandTraits<AtomicRMWInst>::op_begin(this),
1479 OperandTraits<AtomicRMWInst>::operands(this),
1480 InsertAtEnd) {
1481 Init(Operation, Ptr, Val, Ordering, SynchScope);
1482}
1483
1484//===----------------------------------------------------------------------===//
Eli Friedmanfee02c62011-07-25 23:16:38 +00001485// FenceInst Implementation
1486//===----------------------------------------------------------------------===//
1487
1488FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1489 SynchronizationScope SynchScope,
1490 Instruction *InsertBefore)
Craig Topperc6207612014-04-09 06:08:46 +00001491 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertBefore) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001492 setOrdering(Ordering);
1493 setSynchScope(SynchScope);
1494}
1495
1496FenceInst::FenceInst(LLVMContext &C, AtomicOrdering Ordering,
1497 SynchronizationScope SynchScope,
1498 BasicBlock *InsertAtEnd)
Craig Topperc6207612014-04-09 06:08:46 +00001499 : Instruction(Type::getVoidTy(C), Fence, nullptr, 0, InsertAtEnd) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001500 setOrdering(Ordering);
1501 setSynchScope(SynchScope);
1502}
1503
1504//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001505// GetElementPtrInst Implementation
1506//===----------------------------------------------------------------------===//
1507
Pete Cooper1d078692015-12-14 20:29:16 +00001508void GetElementPtrInst::anchor() {}
1509
Jay Foadd1b78492011-07-25 09:48:08 +00001510void GetElementPtrInst::init(Value *Ptr, ArrayRef<Value *> IdxList,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001511 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001512 assert(getNumOperands() == 1 + IdxList.size() &&
1513 "NumOperands not initialized?");
Pete Coopereb31b682015-05-21 22:48:54 +00001514 Op<0>() = Ptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001515 std::copy(IdxList.begin(), IdxList.end(), op_begin() + 1);
Matthijs Kooijman76d8dec2008-06-04 16:14:12 +00001516 setName(Name);
Chris Lattner82981202005-05-03 05:43:30 +00001517}
1518
Gabor Greiff6caff662008-05-10 08:32:32 +00001519GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI)
David Blaikie73cf8722015-05-05 18:03:48 +00001520 : Instruction(GEPI.getType(), GetElementPtr,
1521 OperandTraits<GetElementPtrInst>::op_end(this) -
1522 GEPI.getNumOperands(),
1523 GEPI.getNumOperands()),
David Blaikief5147ef2015-06-01 03:09:34 +00001524 SourceElementType(GEPI.SourceElementType),
1525 ResultElementType(GEPI.ResultElementType) {
Jay Foadd1b78492011-07-25 09:48:08 +00001526 std::copy(GEPI.op_begin(), GEPI.op_end(), op_begin());
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001527 SubclassOptionalData = GEPI.SubclassOptionalData;
Gabor Greiff6caff662008-05-10 08:32:32 +00001528}
1529
Chris Lattner6090a422009-03-09 04:46:40 +00001530/// getIndexedType - Returns the type of the element that would be accessed with
1531/// a gep instruction with the specified parameters.
1532///
1533/// The Idxs pointer should point to a continuous piece of memory containing the
1534/// indices, either as Value* or uint64_t.
1535///
1536/// A null type is returned if the indices are invalid for the specified
1537/// pointer type.
1538///
Matthijs Kooijman04468622008-07-29 08:46:11 +00001539template <typename IndexTy>
David Blaikied288fb82015-03-30 21:41:43 +00001540static Type *getIndexedTypeInternal(Type *Agg, ArrayRef<IndexTy> IdxList) {
Chris Lattner6090a422009-03-09 04:46:40 +00001541 // Handle the special case of the empty set index set, which is always valid.
Jay Foadd1b78492011-07-25 09:48:08 +00001542 if (IdxList.empty())
Dan Gohman12fce772008-05-15 19:50:34 +00001543 return Agg;
Nadav Rotem3924cb02011-12-05 06:29:09 +00001544
Chris Lattner6090a422009-03-09 04:46:40 +00001545 // If there is at least one index, the top level type must be sized, otherwise
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001546 // it cannot be 'stepped over'.
1547 if (!Agg->isSized())
Craig Topperc6207612014-04-09 06:08:46 +00001548 return nullptr;
Misha Brukmanb1c93172005-04-21 23:48:37 +00001549
Dan Gohman1ecaf452008-05-31 00:58:22 +00001550 unsigned CurIdx = 1;
Jay Foadd1b78492011-07-25 09:48:08 +00001551 for (; CurIdx != IdxList.size(); ++CurIdx) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001552 CompositeType *CT = dyn_cast<CompositeType>(Agg);
Craig Topperc6207612014-04-09 06:08:46 +00001553 if (!CT || CT->isPointerTy()) return nullptr;
Jay Foadd1b78492011-07-25 09:48:08 +00001554 IndexTy Index = IdxList[CurIdx];
Craig Topperc6207612014-04-09 06:08:46 +00001555 if (!CT->indexValid(Index)) return nullptr;
Dan Gohman1ecaf452008-05-31 00:58:22 +00001556 Agg = CT->getTypeAtIndex(Index);
Dan Gohman1ecaf452008-05-31 00:58:22 +00001557 }
Craig Topperc6207612014-04-09 06:08:46 +00001558 return CurIdx == IdxList.size() ? Agg : nullptr;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001559}
1560
David Blaikied288fb82015-03-30 21:41:43 +00001561Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<Value *> IdxList) {
1562 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001563}
1564
David Blaikied288fb82015-03-30 21:41:43 +00001565Type *GetElementPtrInst::getIndexedType(Type *Ty,
Jay Foadd1b78492011-07-25 09:48:08 +00001566 ArrayRef<Constant *> IdxList) {
David Blaikied288fb82015-03-30 21:41:43 +00001567 return getIndexedTypeInternal(Ty, IdxList);
Jay Foad1d4a8fe2011-01-14 08:07:43 +00001568}
1569
David Blaikied288fb82015-03-30 21:41:43 +00001570Type *GetElementPtrInst::getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList) {
1571 return getIndexedTypeInternal(Ty, IdxList);
Matthijs Kooijman04468622008-07-29 08:46:11 +00001572}
1573
Chris Lattner45f15572007-04-14 00:12:57 +00001574/// hasAllZeroIndices - Return true if all of the indices of this GEP are
1575/// zeros. If so, the result pointer and the first operand have the same
1576/// value, just potentially different types.
1577bool GetElementPtrInst::hasAllZeroIndices() const {
1578 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1579 if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
1580 if (!CI->isZero()) return false;
1581 } else {
1582 return false;
1583 }
1584 }
1585 return true;
1586}
1587
Chris Lattner27058292007-04-27 20:35:56 +00001588/// hasAllConstantIndices - Return true if all of the indices of this GEP are
1589/// constant integers. If so, the result pointer and the first operand have
1590/// a constant offset between them.
1591bool GetElementPtrInst::hasAllConstantIndices() const {
1592 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1593 if (!isa<ConstantInt>(getOperand(i)))
1594 return false;
1595 }
1596 return true;
1597}
1598
Dan Gohman1b849082009-09-07 23:54:19 +00001599void GetElementPtrInst::setIsInBounds(bool B) {
1600 cast<GEPOperator>(this)->setIsInBounds(B);
1601}
Chris Lattner45f15572007-04-14 00:12:57 +00001602
Nick Lewycky28a5f252009-09-27 21:33:04 +00001603bool GetElementPtrInst::isInBounds() const {
1604 return cast<GEPOperator>(this)->isInBounds();
1605}
1606
Chandler Carruth1e140532012-12-11 10:29:10 +00001607bool GetElementPtrInst::accumulateConstantOffset(const DataLayout &DL,
1608 APInt &Offset) const {
Chandler Carruth7ec41c72012-12-11 11:05:15 +00001609 // Delegate to the generic GEPOperator implementation.
1610 return cast<GEPOperator>(this)->accumulateConstantOffset(DL, Offset);
Chandler Carruth1e140532012-12-11 10:29:10 +00001611}
1612
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001613//===----------------------------------------------------------------------===//
Robert Bocchino23004482006-01-10 19:05:34 +00001614// ExtractElementInst Implementation
1615//===----------------------------------------------------------------------===//
1616
1617ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001618 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001619 Instruction *InsertBef)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001620 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001621 ExtractElement,
1622 OperandTraits<ExtractElementInst>::op_begin(this),
1623 2, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001624 assert(isValidOperands(Val, Index) &&
1625 "Invalid extractelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001626 Op<0>() = Val;
1627 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001628 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001629}
1630
1631ExtractElementInst::ExtractElementInst(Value *Val, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001632 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001633 BasicBlock *InsertAE)
Reid Spencerd84d35b2007-02-15 02:26:10 +00001634 : Instruction(cast<VectorType>(Val->getType())->getElementType(),
Gabor Greiff6caff662008-05-10 08:32:32 +00001635 ExtractElement,
1636 OperandTraits<ExtractElementInst>::op_begin(this),
1637 2, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001638 assert(isValidOperands(Val, Index) &&
1639 "Invalid extractelement instruction operands!");
1640
Gabor Greif2d3024d2008-05-26 21:33:52 +00001641 Op<0>() = Val;
1642 Op<1>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001643 setName(Name);
Robert Bocchino23004482006-01-10 19:05:34 +00001644}
1645
Chris Lattner65511ff2006-10-05 06:24:58 +00001646
Chris Lattner54865b32006-04-08 04:05:48 +00001647bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) {
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001648 if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy())
Chris Lattner54865b32006-04-08 04:05:48 +00001649 return false;
1650 return true;
1651}
1652
1653
Robert Bocchino23004482006-01-10 19:05:34 +00001654//===----------------------------------------------------------------------===//
Robert Bocchinoca27f032006-01-17 20:07:22 +00001655// InsertElementInst Implementation
1656//===----------------------------------------------------------------------===//
1657
Chris Lattner54865b32006-04-08 04:05:48 +00001658InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001659 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001660 Instruction *InsertBef)
Gabor Greiff6caff662008-05-10 08:32:32 +00001661 : Instruction(Vec->getType(), InsertElement,
1662 OperandTraits<InsertElementInst>::op_begin(this),
1663 3, InsertBef) {
Chris Lattner54865b32006-04-08 04:05:48 +00001664 assert(isValidOperands(Vec, Elt, Index) &&
1665 "Invalid insertelement instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001666 Op<0>() = Vec;
1667 Op<1>() = Elt;
1668 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001669 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001670}
1671
Chris Lattner54865b32006-04-08 04:05:48 +00001672InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001673 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001674 BasicBlock *InsertAE)
Gabor Greiff6caff662008-05-10 08:32:32 +00001675 : Instruction(Vec->getType(), InsertElement,
1676 OperandTraits<InsertElementInst>::op_begin(this),
1677 3, InsertAE) {
Chris Lattner54865b32006-04-08 04:05:48 +00001678 assert(isValidOperands(Vec, Elt, Index) &&
1679 "Invalid insertelement instruction operands!");
1680
Gabor Greif2d3024d2008-05-26 21:33:52 +00001681 Op<0>() = Vec;
1682 Op<1>() = Elt;
1683 Op<2>() = Index;
Chris Lattner2195fc42007-02-24 00:55:48 +00001684 setName(Name);
Robert Bocchinoca27f032006-01-17 20:07:22 +00001685}
1686
Chris Lattner54865b32006-04-08 04:05:48 +00001687bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt,
1688 const Value *Index) {
Duncan Sands19d0b472010-02-16 11:11:14 +00001689 if (!Vec->getType()->isVectorTy())
Reid Spencer09575ba2007-02-15 03:39:18 +00001690 return false; // First operand of insertelement must be vector type.
Chris Lattner54865b32006-04-08 04:05:48 +00001691
Reid Spencerd84d35b2007-02-15 02:26:10 +00001692 if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType())
Dan Gohmanfead7972007-05-11 21:43:24 +00001693 return false;// Second operand of insertelement must be vector element type.
Chris Lattner54865b32006-04-08 04:05:48 +00001694
Michael J. Spencer1f10c5ea2014-05-01 22:12:39 +00001695 if (!Index->getType()->isIntegerTy())
Dan Gohman4fe64de2009-06-14 23:30:43 +00001696 return false; // Third operand of insertelement must be i32.
Chris Lattner54865b32006-04-08 04:05:48 +00001697 return true;
1698}
1699
1700
Robert Bocchinoca27f032006-01-17 20:07:22 +00001701//===----------------------------------------------------------------------===//
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001702// ShuffleVectorInst Implementation
1703//===----------------------------------------------------------------------===//
1704
1705ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001706 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001707 Instruction *InsertBefore)
Owen Anderson4056ca92009-07-29 22:17:13 +00001708: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
Mon P Wang25f01062008-11-10 04:46:22 +00001709 cast<VectorType>(Mask->getType())->getNumElements()),
1710 ShuffleVector,
1711 OperandTraits<ShuffleVectorInst>::op_begin(this),
1712 OperandTraits<ShuffleVectorInst>::operands(this),
1713 InsertBefore) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001714 assert(isValidOperands(V1, V2, Mask) &&
1715 "Invalid shuffle vector instruction operands!");
Gabor Greif2d3024d2008-05-26 21:33:52 +00001716 Op<0>() = V1;
1717 Op<1>() = V2;
1718 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001719 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001720}
1721
1722ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
Daniel Dunbar4975db62009-07-25 04:41:11 +00001723 const Twine &Name,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001724 BasicBlock *InsertAtEnd)
Dan Gohmane5af8cd2009-08-25 23:27:45 +00001725: Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(),
1726 cast<VectorType>(Mask->getType())->getNumElements()),
1727 ShuffleVector,
1728 OperandTraits<ShuffleVectorInst>::op_begin(this),
1729 OperandTraits<ShuffleVectorInst>::operands(this),
1730 InsertAtEnd) {
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001731 assert(isValidOperands(V1, V2, Mask) &&
1732 "Invalid shuffle vector instruction operands!");
1733
Gabor Greif2d3024d2008-05-26 21:33:52 +00001734 Op<0>() = V1;
1735 Op<1>() = V2;
1736 Op<2>() = Mask;
Chris Lattner2195fc42007-02-24 00:55:48 +00001737 setName(Name);
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001738}
1739
Mon P Wang25f01062008-11-10 04:46:22 +00001740bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001741 const Value *Mask) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001742 // V1 and V2 must be vectors of the same type.
Duncan Sands19d0b472010-02-16 11:11:14 +00001743 if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType())
Chris Lattnerf724e342008-03-02 05:28:33 +00001744 return false;
1745
Chris Lattner1dcb6542012-01-25 23:49:49 +00001746 // Mask must be vector of i32.
Chris Lattner229907c2011-07-18 04:54:35 +00001747 VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
Craig Topperc6207612014-04-09 06:08:46 +00001748 if (!MaskTy || !MaskTy->getElementType()->isIntegerTy(32))
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001749 return false;
Nate Begeman60a31c32010-08-13 00:16:46 +00001750
1751 // Check to see if Mask is valid.
Chris Lattner1dcb6542012-01-25 23:49:49 +00001752 if (isa<UndefValue>(Mask) || isa<ConstantAggregateZero>(Mask))
1753 return true;
1754
Nate Begeman60a31c32010-08-13 00:16:46 +00001755 if (const ConstantVector *MV = dyn_cast<ConstantVector>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001756 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001757 for (Value *Op : MV->operands()) {
1758 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001759 if (CI->uge(V1Size*2))
Nate Begeman60a31c32010-08-13 00:16:46 +00001760 return false;
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001761 } else if (!isa<UndefValue>(Op)) {
Nate Begeman60a31c32010-08-13 00:16:46 +00001762 return false;
1763 }
1764 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001765 return true;
Mon P Wang6ebf4012011-10-26 00:34:48 +00001766 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001767
1768 if (const ConstantDataSequential *CDS =
1769 dyn_cast<ConstantDataSequential>(Mask)) {
1770 unsigned V1Size = cast<VectorType>(V1->getType())->getNumElements();
1771 for (unsigned i = 0, e = MaskTy->getNumElements(); i != e; ++i)
1772 if (CDS->getElementAsInteger(i) >= V1Size*2)
1773 return false;
1774 return true;
1775 }
1776
1777 // The bitcode reader can create a place holder for a forward reference
1778 // used as the shuffle mask. When this occurs, the shuffle mask will
1779 // fall into this case and fail. To avoid this error, do this bit of
1780 // ugliness to allow such a mask pass.
1781 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(Mask))
1782 if (CE->getOpcode() == Instruction::UserOp1)
1783 return true;
1784
1785 return false;
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001786}
1787
Chris Lattnerf724e342008-03-02 05:28:33 +00001788/// getMaskValue - Return the index from the shuffle mask for the specified
1789/// output result. This is either -1 if the element is undef or a number less
1790/// than 2*numelements.
Chris Lattnercf129702012-01-26 02:51:13 +00001791int ShuffleVectorInst::getMaskValue(Constant *Mask, unsigned i) {
1792 assert(i < Mask->getType()->getVectorNumElements() && "Index out of range");
1793 if (ConstantDataSequential *CDS =dyn_cast<ConstantDataSequential>(Mask))
Chris Lattner1dcb6542012-01-25 23:49:49 +00001794 return CDS->getElementAsInteger(i);
Chris Lattnercf129702012-01-26 02:51:13 +00001795 Constant *C = Mask->getAggregateElement(i);
Chris Lattner1dcb6542012-01-25 23:49:49 +00001796 if (isa<UndefValue>(C))
Chris Lattnerf724e342008-03-02 05:28:33 +00001797 return -1;
Chris Lattner1dcb6542012-01-25 23:49:49 +00001798 return cast<ConstantInt>(C)->getZExtValue();
Chris Lattnerf724e342008-03-02 05:28:33 +00001799}
1800
Chris Lattner1dcb6542012-01-25 23:49:49 +00001801/// getShuffleMask - Return the full mask for this instruction, where each
1802/// element is the element number and undef's are returned as -1.
Chris Lattnercf129702012-01-26 02:51:13 +00001803void ShuffleVectorInst::getShuffleMask(Constant *Mask,
1804 SmallVectorImpl<int> &Result) {
1805 unsigned NumElts = Mask->getType()->getVectorNumElements();
Chris Lattner1dcb6542012-01-25 23:49:49 +00001806
Chris Lattnercf129702012-01-26 02:51:13 +00001807 if (ConstantDataSequential *CDS=dyn_cast<ConstantDataSequential>(Mask)) {
Chris Lattner1dcb6542012-01-25 23:49:49 +00001808 for (unsigned i = 0; i != NumElts; ++i)
1809 Result.push_back(CDS->getElementAsInteger(i));
1810 return;
1811 }
Chris Lattner1dcb6542012-01-25 23:49:49 +00001812 for (unsigned i = 0; i != NumElts; ++i) {
1813 Constant *C = Mask->getAggregateElement(i);
1814 Result.push_back(isa<UndefValue>(C) ? -1 :
Chris Lattner3dbad4032012-01-26 00:41:50 +00001815 cast<ConstantInt>(C)->getZExtValue());
Chris Lattner1dcb6542012-01-25 23:49:49 +00001816 }
1817}
1818
1819
Dan Gohman12fce772008-05-15 19:50:34 +00001820//===----------------------------------------------------------------------===//
Dan Gohman0752bff2008-05-23 00:36:11 +00001821// InsertValueInst Class
1822//===----------------------------------------------------------------------===//
1823
Jay Foad57aa6362011-07-13 10:26:04 +00001824void InsertValueInst::init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1825 const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001826 assert(getNumOperands() == 2 && "NumOperands not initialized?");
Jay Foad57aa6362011-07-13 10:26:04 +00001827
1828 // There's no fundamental reason why we require at least one index
1829 // (other than weirdness with &*IdxBegin being invalid; see
1830 // getelementptr's init routine for example). But there's no
1831 // present need to support it.
1832 assert(Idxs.size() > 0 && "InsertValueInst must have at least one index");
1833
1834 assert(ExtractValueInst::getIndexedType(Agg->getType(), Idxs) ==
Frits van Bommel16ebe772010-12-05 20:50:26 +00001835 Val->getType() && "Inserted value must match indexed type!");
Dan Gohman1ecaf452008-05-31 00:58:22 +00001836 Op<0>() = Agg;
1837 Op<1>() = Val;
Dan Gohman0752bff2008-05-23 00:36:11 +00001838
Jay Foad57aa6362011-07-13 10:26:04 +00001839 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001840 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001841}
1842
1843InsertValueInst::InsertValueInst(const InsertValueInst &IVI)
Gabor Greife9408e62008-05-27 11:03:29 +00001844 : Instruction(IVI.getType(), InsertValue,
Dan Gohman1ecaf452008-05-31 00:58:22 +00001845 OperandTraits<InsertValueInst>::op_begin(this), 2),
1846 Indices(IVI.Indices) {
Dan Gohmand8ca05f2008-06-17 23:25:49 +00001847 Op<0>() = IVI.getOperand(0);
1848 Op<1>() = IVI.getOperand(1);
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001849 SubclassOptionalData = IVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001850}
1851
1852//===----------------------------------------------------------------------===//
Dan Gohman12fce772008-05-15 19:50:34 +00001853// ExtractValueInst Class
1854//===----------------------------------------------------------------------===//
1855
Jay Foad57aa6362011-07-13 10:26:04 +00001856void ExtractValueInst::init(ArrayRef<unsigned> Idxs, const Twine &Name) {
Pete Cooperb4eede22015-06-12 17:48:10 +00001857 assert(getNumOperands() == 1 && "NumOperands not initialized?");
Dan Gohman0752bff2008-05-23 00:36:11 +00001858
Jay Foad57aa6362011-07-13 10:26:04 +00001859 // There's no fundamental reason why we require at least one index.
1860 // But there's no present need to support it.
1861 assert(Idxs.size() > 0 && "ExtractValueInst must have at least one index");
Dan Gohman0752bff2008-05-23 00:36:11 +00001862
Jay Foad57aa6362011-07-13 10:26:04 +00001863 Indices.append(Idxs.begin(), Idxs.end());
Matthijs Kooijmancfd41db2008-06-04 14:40:55 +00001864 setName(Name);
Dan Gohman0752bff2008-05-23 00:36:11 +00001865}
1866
1867ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI)
Gabor Greif21ba1842008-06-06 20:28:12 +00001868 : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)),
Dan Gohman1ecaf452008-05-31 00:58:22 +00001869 Indices(EVI.Indices) {
Dan Gohmanc8a27f22009-08-25 22:11:20 +00001870 SubclassOptionalData = EVI.SubclassOptionalData;
Dan Gohman0752bff2008-05-23 00:36:11 +00001871}
1872
Dan Gohman12fce772008-05-15 19:50:34 +00001873// getIndexedType - Returns the type of the element that would be extracted
1874// with an extractvalue instruction with the specified parameters.
1875//
1876// A null type is returned if the indices are invalid for the specified
1877// pointer type.
1878//
Chris Lattner229907c2011-07-18 04:54:35 +00001879Type *ExtractValueInst::getIndexedType(Type *Agg,
Jay Foad57aa6362011-07-13 10:26:04 +00001880 ArrayRef<unsigned> Idxs) {
Benjamin Kramer3ad5c962014-03-10 15:03:06 +00001881 for (unsigned Index : Idxs) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001882 // We can't use CompositeType::indexValid(Index) here.
1883 // indexValid() always returns true for arrays because getelementptr allows
1884 // out-of-bounds indices. Since we don't allow those for extractvalue and
1885 // insertvalue we need to check array indexing manually.
1886 // Since the only other types we can index into are struct types it's just
1887 // as easy to check those manually as well.
Chris Lattner229907c2011-07-18 04:54:35 +00001888 if (ArrayType *AT = dyn_cast<ArrayType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001889 if (Index >= AT->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001890 return nullptr;
Chris Lattner229907c2011-07-18 04:54:35 +00001891 } else if (StructType *ST = dyn_cast<StructType>(Agg)) {
Frits van Bommel16ebe772010-12-05 20:50:26 +00001892 if (Index >= ST->getNumElements())
Craig Topperc6207612014-04-09 06:08:46 +00001893 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001894 } else {
1895 // Not a valid type to index into.
Craig Topperc6207612014-04-09 06:08:46 +00001896 return nullptr;
Frits van Bommel16ebe772010-12-05 20:50:26 +00001897 }
1898
1899 Agg = cast<CompositeType>(Agg)->getTypeAtIndex(Index);
Dan Gohman12fce772008-05-15 19:50:34 +00001900 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001901 return const_cast<Type*>(Agg);
Dan Gohman12fce772008-05-15 19:50:34 +00001902}
Chris Lattnerbbe0a422006-04-08 01:18:18 +00001903
1904//===----------------------------------------------------------------------===//
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001905// BinaryOperator Class
1906//===----------------------------------------------------------------------===//
1907
Chris Lattner2195fc42007-02-24 00:55:48 +00001908BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001909 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001910 Instruction *InsertBefore)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001911 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001912 OperandTraits<BinaryOperator>::op_begin(this),
1913 OperandTraits<BinaryOperator>::operands(this),
1914 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001915 Op<0>() = S1;
1916 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001917 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001918 setName(Name);
1919}
1920
1921BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2,
Chris Lattner229907c2011-07-18 04:54:35 +00001922 Type *Ty, const Twine &Name,
Chris Lattner2195fc42007-02-24 00:55:48 +00001923 BasicBlock *InsertAtEnd)
Dan Gohmana2414ea2010-05-03 22:44:19 +00001924 : Instruction(Ty, iType,
Gabor Greiff6caff662008-05-10 08:32:32 +00001925 OperandTraits<BinaryOperator>::op_begin(this),
1926 OperandTraits<BinaryOperator>::operands(this),
1927 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00001928 Op<0>() = S1;
1929 Op<1>() = S2;
Dan Gohmana2414ea2010-05-03 22:44:19 +00001930 init(iType);
Chris Lattner2195fc42007-02-24 00:55:48 +00001931 setName(Name);
1932}
1933
1934
1935void BinaryOperator::init(BinaryOps iType) {
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001936 Value *LHS = getOperand(0), *RHS = getOperand(1);
Jeffrey Yasskin9b43f332010-12-23 00:58:24 +00001937 (void)LHS; (void)RHS; // Silence warnings.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001938 assert(LHS->getType() == RHS->getType() &&
1939 "Binary operator operand types must match!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001940#ifndef NDEBUG
1941 switch (iType) {
1942 case Add: case Sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00001943 case Mul:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001944 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001945 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001946 assert(getType()->isIntOrIntVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001947 "Tried to create an integer operation on a non-integer type!");
1948 break;
1949 case FAdd: case FSub:
1950 case FMul:
1951 assert(getType() == LHS->getType() &&
1952 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001953 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohmana5b96452009-06-04 22:49:04 +00001954 "Tried to create a floating-point operation on a "
1955 "non-floating-point type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001956 break;
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001957 case UDiv:
1958 case SDiv:
1959 assert(getType() == LHS->getType() &&
1960 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001961 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001962 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001963 "Incorrect operand type (not integer) for S/UDIV");
1964 break;
1965 case FDiv:
1966 assert(getType() == LHS->getType() &&
1967 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001968 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001969 "Incorrect operand type (not floating point) for FDIV");
Reid Spencer7e80b0b2006-10-26 06:15:43 +00001970 break;
Reid Spencer7eb55b32006-11-02 01:53:59 +00001971 case URem:
1972 case SRem:
1973 assert(getType() == LHS->getType() &&
1974 "Arithmetic operation should return same type as operands!");
Duncan Sands19d0b472010-02-16 11:11:14 +00001975 assert((getType()->isIntegerTy() || (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001976 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Reid Spencer7eb55b32006-11-02 01:53:59 +00001977 "Incorrect operand type (not integer) for S/UREM");
1978 break;
1979 case FRem:
1980 assert(getType() == LHS->getType() &&
1981 "Arithmetic operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001982 assert(getType()->isFPOrFPVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00001983 "Incorrect operand type (not floating point) for FREM");
Reid Spencer7eb55b32006-11-02 01:53:59 +00001984 break;
Reid Spencer2341c222007-02-02 02:16:23 +00001985 case Shl:
1986 case LShr:
1987 case AShr:
1988 assert(getType() == LHS->getType() &&
1989 "Shift operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001990 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00001991 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00001992 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Nate Begemanfecbc8c2008-07-29 15:49:41 +00001993 "Tried to create a shift operation on a non-integral type!");
Reid Spencer2341c222007-02-02 02:16:23 +00001994 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001995 case And: case Or:
1996 case Xor:
Chris Lattnerafdb3de2005-01-29 00:35:16 +00001997 assert(getType() == LHS->getType() &&
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00001998 "Logical operation should return same type as operands!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00001999 assert((getType()->isIntegerTy() ||
Duncan Sands19d0b472010-02-16 11:11:14 +00002000 (getType()->isVectorTy() &&
Duncan Sands9dff9be2010-02-15 16:12:20 +00002001 cast<VectorType>(getType())->getElementType()->isIntegerTy())) &&
Misha Brukman3852f652005-01-27 06:46:38 +00002002 "Tried to create a logical operation on a non-integral type!");
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002003 break;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002004 default:
2005 break;
2006 }
2007#endif
2008}
2009
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002010BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002011 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002012 Instruction *InsertBefore) {
2013 assert(S1->getType() == S2->getType() &&
2014 "Cannot create binary operator with two operands of differing type!");
Reid Spencer266e42b2006-12-23 06:05:41 +00002015 return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002016}
2017
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002018BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002019 const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002020 BasicBlock *InsertAtEnd) {
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002021 BinaryOperator *Res = Create(Op, S1, S2, Name);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002022 InsertAtEnd->getInstList().push_back(Res);
2023 return Res;
2024}
2025
Dan Gohman5476cfd2009-08-12 16:23:25 +00002026BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002027 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002028 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002029 return new BinaryOperator(Instruction::Sub,
2030 zero, Op,
2031 Op->getType(), Name, InsertBefore);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002032}
2033
Dan Gohman5476cfd2009-08-12 16:23:25 +00002034BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002035 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002036 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Reid Spencer2eadb532007-01-21 00:29:26 +00002037 return new BinaryOperator(Instruction::Sub,
2038 zero, Op,
2039 Op->getType(), Name, InsertAtEnd);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002040}
2041
Dan Gohman4ab44202009-12-18 02:58:50 +00002042BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2043 Instruction *InsertBefore) {
2044 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2045 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore);
2046}
2047
2048BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name,
2049 BasicBlock *InsertAtEnd) {
2050 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2051 return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd);
2052}
2053
Duncan Sandsfa5f5962010-02-02 12:53:04 +00002054BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2055 Instruction *InsertBefore) {
2056 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2057 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore);
2058}
2059
2060BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name,
2061 BasicBlock *InsertAtEnd) {
2062 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
2063 return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd);
2064}
2065
Dan Gohman5476cfd2009-08-12 16:23:25 +00002066BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002067 Instruction *InsertBefore) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002068 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002069 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002070 Op->getType(), Name, InsertBefore);
2071}
2072
Dan Gohman5476cfd2009-08-12 16:23:25 +00002073BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name,
Dan Gohmana5b96452009-06-04 22:49:04 +00002074 BasicBlock *InsertAtEnd) {
Owen Anderson69c464d2009-07-27 20:59:43 +00002075 Value *zero = ConstantFP::getZeroValueForNegation(Op->getType());
Chris Lattner47a86bd2012-01-25 06:02:56 +00002076 return new BinaryOperator(Instruction::FSub, zero, Op,
Dan Gohmana5b96452009-06-04 22:49:04 +00002077 Op->getType(), Name, InsertAtEnd);
2078}
2079
Dan Gohman5476cfd2009-08-12 16:23:25 +00002080BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002081 Instruction *InsertBefore) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002082 Constant *C = Constant::getAllOnesValue(Op->getType());
Chris Lattnere8e7ac42006-03-25 21:54:21 +00002083 return new BinaryOperator(Instruction::Xor, Op, C,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002084 Op->getType(), Name, InsertBefore);
2085}
2086
Dan Gohman5476cfd2009-08-12 16:23:25 +00002087BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002088 BasicBlock *InsertAtEnd) {
Chris Lattner47a86bd2012-01-25 06:02:56 +00002089 Constant *AllOnes = Constant::getAllOnesValue(Op->getType());
Chris Lattnerdca56cb2005-12-21 18:22:19 +00002090 return new BinaryOperator(Instruction::Xor, Op, AllOnes,
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002091 Op->getType(), Name, InsertAtEnd);
2092}
2093
2094
2095// isConstantAllOnes - Helper function for several functions below
2096static inline bool isConstantAllOnes(const Value *V) {
Chris Lattner0256be92012-01-27 03:08:05 +00002097 if (const Constant *C = dyn_cast<Constant>(V))
2098 return C->isAllOnesValue();
Chris Lattner1edec382007-06-15 06:04:24 +00002099 return false;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002100}
2101
Owen Andersonbb2501b2009-07-13 22:18:28 +00002102bool BinaryOperator::isNeg(const Value *V) {
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002103 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2104 if (Bop->getOpcode() == Instruction::Sub)
Owen Andersonbb2501b2009-07-13 22:18:28 +00002105 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0)))
2106 return C->isNegativeZeroValue();
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002107 return false;
2108}
2109
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002110bool BinaryOperator::isFNeg(const Value *V, bool IgnoreZeroSign) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002111 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2112 if (Bop->getOpcode() == Instruction::FSub)
Shuxin Yangf0537ab2013-01-09 00:13:41 +00002113 if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) {
2114 if (!IgnoreZeroSign)
2115 IgnoreZeroSign = cast<Instruction>(V)->hasNoSignedZeros();
2116 return !IgnoreZeroSign ? C->isNegativeZeroValue() : C->isZeroValue();
2117 }
Dan Gohmana5b96452009-06-04 22:49:04 +00002118 return false;
2119}
2120
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002121bool BinaryOperator::isNot(const Value *V) {
2122 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V))
2123 return (Bop->getOpcode() == Instruction::Xor &&
2124 (isConstantAllOnes(Bop->getOperand(1)) ||
2125 isConstantAllOnes(Bop->getOperand(0))));
2126 return false;
2127}
2128
Chris Lattner2c7d1772005-04-24 07:28:37 +00002129Value *BinaryOperator::getNegArgument(Value *BinOp) {
Chris Lattner2c7d1772005-04-24 07:28:37 +00002130 return cast<BinaryOperator>(BinOp)->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002131}
2132
Chris Lattner2c7d1772005-04-24 07:28:37 +00002133const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
2134 return getNegArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002135}
2136
Dan Gohmana5b96452009-06-04 22:49:04 +00002137Value *BinaryOperator::getFNegArgument(Value *BinOp) {
Dan Gohmana5b96452009-06-04 22:49:04 +00002138 return cast<BinaryOperator>(BinOp)->getOperand(1);
2139}
2140
2141const Value *BinaryOperator::getFNegArgument(const Value *BinOp) {
2142 return getFNegArgument(const_cast<Value*>(BinOp));
2143}
2144
Chris Lattner2c7d1772005-04-24 07:28:37 +00002145Value *BinaryOperator::getNotArgument(Value *BinOp) {
2146 assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
2147 BinaryOperator *BO = cast<BinaryOperator>(BinOp);
2148 Value *Op0 = BO->getOperand(0);
2149 Value *Op1 = BO->getOperand(1);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002150 if (isConstantAllOnes(Op0)) return Op1;
2151
2152 assert(isConstantAllOnes(Op1));
2153 return Op0;
2154}
2155
Chris Lattner2c7d1772005-04-24 07:28:37 +00002156const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
2157 return getNotArgument(const_cast<Value*>(BinOp));
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002158}
2159
2160
2161// swapOperands - Exchange the two operands to this instruction. This
2162// instruction is safe to use on any binary instruction and does not
2163// modify the semantics of the instruction. If the instruction is
2164// order dependent (SetLT f.e.) the opcode is changed.
2165//
2166bool BinaryOperator::swapOperands() {
Reid Spencer266e42b2006-12-23 06:05:41 +00002167 if (!isCommutative())
2168 return true; // Can't commute operands
Gabor Greif5ef74042008-05-13 22:51:52 +00002169 Op<0>().swap(Op<1>());
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00002170 return false;
2171}
2172
Dan Gohman1b849082009-09-07 23:54:19 +00002173void BinaryOperator::setHasNoUnsignedWrap(bool b) {
2174 cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b);
2175}
2176
2177void BinaryOperator::setHasNoSignedWrap(bool b) {
2178 cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b);
2179}
2180
2181void BinaryOperator::setIsExact(bool b) {
Chris Lattner35315d02011-02-06 21:44:57 +00002182 cast<PossiblyExactOperator>(this)->setIsExact(b);
Dan Gohman1b849082009-09-07 23:54:19 +00002183}
2184
Nick Lewycky28a5f252009-09-27 21:33:04 +00002185bool BinaryOperator::hasNoUnsignedWrap() const {
2186 return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap();
2187}
2188
2189bool BinaryOperator::hasNoSignedWrap() const {
2190 return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap();
2191}
2192
2193bool BinaryOperator::isExact() const {
Chris Lattner35315d02011-02-06 21:44:57 +00002194 return cast<PossiblyExactOperator>(this)->isExact();
Nick Lewycky28a5f252009-09-27 21:33:04 +00002195}
2196
Sanjay Patela982d992014-09-03 01:06:50 +00002197void BinaryOperator::copyIRFlags(const Value *V) {
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002198 // Copy the wrapping flags.
2199 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2200 setHasNoSignedWrap(OB->hasNoSignedWrap());
2201 setHasNoUnsignedWrap(OB->hasNoUnsignedWrap());
2202 }
2203
2204 // Copy the exact flag.
2205 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2206 setIsExact(PE->isExact());
2207
2208 // Copy the fast-math flags.
2209 if (auto *FP = dyn_cast<FPMathOperator>(V))
Sanjay Patelb2325b92014-09-02 20:03:00 +00002210 copyFastMathFlags(FP->getFastMathFlags());
Sanjay Patel5ad239e2014-09-01 18:44:57 +00002211}
2212
Sanjay Patela982d992014-09-03 01:06:50 +00002213void BinaryOperator::andIRFlags(const Value *V) {
2214 if (auto *OB = dyn_cast<OverflowingBinaryOperator>(V)) {
2215 setHasNoSignedWrap(hasNoSignedWrap() & OB->hasNoSignedWrap());
2216 setHasNoUnsignedWrap(hasNoUnsignedWrap() & OB->hasNoUnsignedWrap());
2217 }
2218
2219 if (auto *PE = dyn_cast<PossiblyExactOperator>(V))
2220 setIsExact(isExact() & PE->isExact());
2221
2222 if (auto *FP = dyn_cast<FPMathOperator>(V)) {
2223 FastMathFlags FM = getFastMathFlags();
2224 FM &= FP->getFastMathFlags();
2225 copyFastMathFlags(FM);
2226 }
2227}
2228
2229
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002230//===----------------------------------------------------------------------===//
Duncan Sands05f4df82012-04-16 16:28:59 +00002231// FPMathOperator Class
2232//===----------------------------------------------------------------------===//
2233
2234/// getFPAccuracy - Get the maximum error permitted by this operation in ULPs.
2235/// An accuracy of 0.0 means that the operation should be performed with the
Duncan Sands9af62982012-04-16 19:39:33 +00002236/// default precision.
Duncan Sands05f4df82012-04-16 16:28:59 +00002237float FPMathOperator::getFPAccuracy() const {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002238 const MDNode *MD =
2239 cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
Duncan Sands05f4df82012-04-16 16:28:59 +00002240 if (!MD)
2241 return 0.0;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002242 ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
Duncan Sands9af62982012-04-16 19:39:33 +00002243 return Accuracy->getValueAPF().convertToFloat();
Duncan Sands05f4df82012-04-16 16:28:59 +00002244}
2245
2246
2247//===----------------------------------------------------------------------===//
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002248// CastInst Class
2249//===----------------------------------------------------------------------===//
2250
David Blaikie3a15e142011-12-01 08:00:17 +00002251void CastInst::anchor() {}
2252
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002253// Just determine if this cast only deals with integral->integral conversion.
2254bool CastInst::isIntegerCast() const {
2255 switch (getOpcode()) {
2256 default: return false;
2257 case Instruction::ZExt:
2258 case Instruction::SExt:
2259 case Instruction::Trunc:
2260 return true;
2261 case Instruction::BitCast:
Duncan Sands9dff9be2010-02-15 16:12:20 +00002262 return getOperand(0)->getType()->isIntegerTy() &&
2263 getType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002264 }
Chris Lattnerb0b8ddd2006-09-18 04:54:57 +00002265}
2266
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002267bool CastInst::isLosslessCast() const {
2268 // Only BitCast can be lossless, exit fast if we're not BitCast
2269 if (getOpcode() != Instruction::BitCast)
2270 return false;
2271
2272 // Identity cast is always lossless
Chris Lattner229907c2011-07-18 04:54:35 +00002273 Type* SrcTy = getOperand(0)->getType();
2274 Type* DstTy = getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002275 if (SrcTy == DstTy)
2276 return true;
2277
Reid Spencer8d9336d2006-12-31 05:26:44 +00002278 // Pointer to pointer is always lossless.
Duncan Sands19d0b472010-02-16 11:11:14 +00002279 if (SrcTy->isPointerTy())
2280 return DstTy->isPointerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002281 return false; // Other types have no identity values
2282}
2283
2284/// This function determines if the CastInst does not require any bits to be
2285/// changed in order to effect the cast. Essentially, it identifies cases where
2286/// no code gen is necessary for the cast, hence the name no-op cast. For
2287/// example, the following are all no-op casts:
Dan Gohmane9bc2ba2008-05-12 16:34:30 +00002288/// # bitcast i32* %x to i8*
2289/// # bitcast <2 x i32> %x to <4 x i16>
2290/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002291/// @brief Determine if the described cast is a no-op.
2292bool CastInst::isNoopCast(Instruction::CastOps Opcode,
Chris Lattner229907c2011-07-18 04:54:35 +00002293 Type *SrcTy,
2294 Type *DestTy,
2295 Type *IntPtrTy) {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002296 switch (Opcode) {
Craig Topperc514b542012-02-05 22:14:15 +00002297 default: llvm_unreachable("Invalid CastOp");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002298 case Instruction::Trunc:
2299 case Instruction::ZExt:
2300 case Instruction::SExt:
2301 case Instruction::FPTrunc:
2302 case Instruction::FPExt:
2303 case Instruction::UIToFP:
2304 case Instruction::SIToFP:
2305 case Instruction::FPToUI:
2306 case Instruction::FPToSI:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002307 case Instruction::AddrSpaceCast:
2308 // TODO: Target informations may give a more accurate answer here.
2309 return false;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002310 case Instruction::BitCast:
2311 return true; // BitCast never modifies bits.
2312 case Instruction::PtrToInt:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002313 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002314 DestTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002315 case Instruction::IntToPtr:
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002316 return IntPtrTy->getScalarSizeInBits() ==
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002317 SrcTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002318 }
2319}
2320
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002321/// @brief Determine if a cast is a no-op.
Chris Lattner229907c2011-07-18 04:54:35 +00002322bool CastInst::isNoopCast(Type *IntPtrTy) const {
Dan Gohman0d7f3b82010-05-28 21:41:37 +00002323 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2324}
2325
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002326bool CastInst::isNoopCast(const DataLayout &DL) const {
Craig Topperc6207612014-04-09 06:08:46 +00002327 Type *PtrOpTy = nullptr;
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002328 if (getOpcode() == Instruction::PtrToInt)
2329 PtrOpTy = getOperand(0)->getType();
2330 else if (getOpcode() == Instruction::IntToPtr)
2331 PtrOpTy = getType();
2332
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002333 Type *IntPtrTy =
2334 PtrOpTy ? DL.getIntPtrType(PtrOpTy) : DL.getIntPtrType(getContext(), 0);
Matt Arsenaulta236ea52014-03-06 17:33:55 +00002335
2336 return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy);
2337}
2338
2339/// This function determines if a pair of casts can be eliminated and what
2340/// opcode should be used in the elimination. This assumes that there are two
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002341/// instructions like this:
2342/// * %F = firstOpcode SrcTy %x to MidTy
2343/// * %S = secondOpcode MidTy %F to DstTy
2344/// The function returns a resultOpcode so these two casts can be replaced with:
2345/// * %Replacement = resultOpcode %SrcTy %x to DstTy
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002346/// If no such cast is permitted, the function returns 0.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002347unsigned CastInst::isEliminableCastPair(
2348 Instruction::CastOps firstOp, Instruction::CastOps secondOp,
Duncan Sandse2395dc2012-10-30 16:03:32 +00002349 Type *SrcTy, Type *MidTy, Type *DstTy, Type *SrcIntPtrTy, Type *MidIntPtrTy,
2350 Type *DstIntPtrTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002351 // Define the 144 possibilities for these two cast instructions. The values
2352 // in this matrix determine what to do in a given situation and select the
2353 // case in the switch below. The rows correspond to firstOp, the columns
Sanjay Patel4dad27e2015-12-11 18:12:01 +00002354 // correspond to secondOp. In looking at the table below, keep in mind
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002355 // the following cast properties:
2356 //
2357 // Size Compare Source Destination
2358 // Operator Src ? Size Type Sign Type Sign
2359 // -------- ------------ ------------------- ---------------------
2360 // TRUNC > Integer Any Integral Any
2361 // ZEXT < Integral Unsigned Integer Any
2362 // SEXT < Integral Signed Integer Any
2363 // FPTOUI n/a FloatPt n/a Integral Unsigned
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002364 // FPTOSI n/a FloatPt n/a Integral Signed
2365 // UITOFP n/a Integral Unsigned FloatPt n/a
2366 // SITOFP n/a Integral Signed FloatPt n/a
2367 // FPTRUNC > FloatPt n/a FloatPt n/a
2368 // FPEXT < FloatPt n/a FloatPt n/a
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002369 // PTRTOINT n/a Pointer n/a Integral Unsigned
2370 // INTTOPTR n/a Integral Unsigned Pointer n/a
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002371 // BITCAST = FirstClass n/a FirstClass n/a
2372 // ADDRSPCST n/a Pointer n/a Pointer n/a
Chris Lattner6f6b4972006-12-05 23:43:59 +00002373 //
2374 // NOTE: some transforms are safe, but we consider them to be non-profitable.
Dan Gohman4fe64de2009-06-14 23:30:43 +00002375 // For example, we could merge "fptoui double to i32" + "zext i32 to i64",
2376 // into "fptoui double to i64", but this loses information about the range
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002377 // of the produced value (we no longer know the top-part is all zeros).
Chris Lattner6f6b4972006-12-05 23:43:59 +00002378 // Further this conversion is often much more expensive for typical hardware,
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002379 // and causes issues when building libgcc. We disallow fptosi+sext for the
Chris Lattner6f6b4972006-12-05 23:43:59 +00002380 // same reason.
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002381 const unsigned numCastOps =
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002382 Instruction::CastOpsEnd - Instruction::CastOpsBegin;
2383 static const uint8_t CastResults[numCastOps][numCastOps] = {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002384 // T F F U S F F P I B A -+
2385 // R Z S P P I I T P 2 N T S |
2386 // U E E 2 2 2 2 R E I T C C +- secondOp
2387 // N X X U S F F N X N 2 V V |
2388 // C T T I I P P C T T P T T -+
2389 { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // Trunc -+
Fiona Glaser0d41db12015-04-21 00:05:41 +00002390 { 8, 1, 9,99,99, 2,17,99,99,99, 2, 3, 0}, // ZExt |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002391 { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3, 0}, // SExt |
2392 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToUI |
2393 { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3, 0}, // FPToSI |
2394 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // UIToFP +- firstOp
2395 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // SIToFP |
Ahmed Bougacha0ea9d1e2015-05-29 00:04:30 +00002396 { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4, 0}, // FPTrunc |
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002397 { 99,99,99, 2, 2,99,99,10, 2,99,99, 4, 0}, // FPExt |
2398 { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3, 0}, // PtrToInt |
2399 { 99,99,99,99,99,99,99,99,99,11,99,15, 0}, // IntToPtr |
2400 { 5, 5, 5, 6, 6, 5, 5, 6, 6,16, 5, 1,14}, // BitCast |
2401 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,13,12}, // AddrSpaceCast -+
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002402 };
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002403
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002404 // TODO: This logic could be encoded into the table above and handled in the
2405 // switch below.
Chris Lattner25eea4d2010-07-12 01:19:22 +00002406 // If either of the casts are a bitcast from scalar to vector, disallow the
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002407 // merging. However, any pair of bitcasts are allowed.
2408 bool IsFirstBitcast = (firstOp == Instruction::BitCast);
2409 bool IsSecondBitcast = (secondOp == Instruction::BitCast);
2410 bool AreBothBitcasts = IsFirstBitcast && IsSecondBitcast;
Nadav Rotem5fc81ff2011-08-29 19:58:36 +00002411
Sanjay Patel93f55dd2015-12-12 00:33:36 +00002412 // Check if any of the casts convert scalars <-> vectors.
2413 if ((IsFirstBitcast && isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) ||
2414 (IsSecondBitcast && isa<VectorType>(MidTy) != isa<VectorType>(DstTy)))
2415 if (!AreBothBitcasts)
2416 return 0;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002417
2418 int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin]
2419 [secondOp-Instruction::CastOpsBegin];
2420 switch (ElimCase) {
2421 case 0:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002422 // Categorically disallowed.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002423 return 0;
2424 case 1:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002425 // Allowed, use first cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002426 return firstOp;
2427 case 2:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002428 // Allowed, use second cast's opcode.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002429 return secondOp;
2430 case 3:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002431 // No-op cast in second op implies firstOp as long as the DestTy
Mon P Wange04b4562010-01-23 04:35:57 +00002432 // is integer and we are not converting between a vector and a
Alp Tokerf907b892013-12-05 05:44:44 +00002433 // non-vector type.
Duncan Sands19d0b472010-02-16 11:11:14 +00002434 if (!SrcTy->isVectorTy() && DstTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002435 return firstOp;
2436 return 0;
2437 case 4:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002438 // No-op cast in second op implies firstOp as long as the DestTy
Chris Lattner531732b2010-01-23 04:42:42 +00002439 // is floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002440 if (DstTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002441 return firstOp;
2442 return 0;
2443 case 5:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002444 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002445 // is an integer.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002446 if (SrcTy->isIntegerTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002447 return secondOp;
2448 return 0;
2449 case 6:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002450 // No-op cast in first op implies secondOp as long as the SrcTy
Chris Lattner531732b2010-01-23 04:42:42 +00002451 // is a floating point.
Duncan Sands9dff9be2010-02-15 16:12:20 +00002452 if (SrcTy->isFloatingPointTy())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002453 return secondOp;
2454 return 0;
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002455 case 7: {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002456 // Cannot simplify if address spaces are different!
2457 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2458 return 0;
2459
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002460 unsigned MidSize = MidTy->getScalarSizeInBits();
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002461 // We can still fold this without knowing the actual sizes as long we
2462 // know that the intermediate pointer is the largest possible
2463 // pointer size.
2464 // FIXME: Is this always true?
2465 if (MidSize == 64)
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002466 return Instruction::BitCast;
2467
2468 // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size.
Duncan Sandse2395dc2012-10-30 16:03:32 +00002469 if (!SrcIntPtrTy || DstIntPtrTy != SrcIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002470 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002471 unsigned PtrSize = SrcIntPtrTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002472 if (MidSize >= PtrSize)
2473 return Instruction::BitCast;
2474 return 0;
2475 }
2476 case 8: {
2477 // ext, trunc -> bitcast, if the SrcTy and DstTy are same size
2478 // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy)
2479 // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy)
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002480 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2481 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002482 if (SrcSize == DstSize)
2483 return Instruction::BitCast;
2484 else if (SrcSize < DstSize)
2485 return firstOp;
2486 return secondOp;
2487 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002488 case 9:
2489 // zext, sext -> zext, because sext can't sign extend after zext
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002490 return Instruction::ZExt;
2491 case 10:
2492 // fpext followed by ftrunc is allowed if the bit size returned to is
2493 // the same as the original, in which case its just a bitcast
2494 if (SrcTy == DstTy)
2495 return Instruction::BitCast;
2496 return 0; // If the types are not the same we can't eliminate it.
Matt Arsenault130e0ef2013-07-30 22:27:10 +00002497 case 11: {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002498 // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize
Duncan Sandse2395dc2012-10-30 16:03:32 +00002499 if (!MidIntPtrTy)
Dan Gohman9413de12009-07-21 23:19:40 +00002500 return 0;
Duncan Sandse2395dc2012-10-30 16:03:32 +00002501 unsigned PtrSize = MidIntPtrTy->getScalarSizeInBits();
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002502 unsigned SrcSize = SrcTy->getScalarSizeInBits();
2503 unsigned DstSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002504 if (SrcSize <= PtrSize && SrcSize == DstSize)
2505 return Instruction::BitCast;
2506 return 0;
2507 }
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002508 case 12: {
2509 // addrspacecast, addrspacecast -> bitcast, if SrcAS == DstAS
2510 // addrspacecast, addrspacecast -> addrspacecast, if SrcAS != DstAS
2511 if (SrcTy->getPointerAddressSpace() != DstTy->getPointerAddressSpace())
2512 return Instruction::AddrSpaceCast;
2513 return Instruction::BitCast;
2514 }
2515 case 13:
2516 // FIXME: this state can be merged with (1), but the following assert
2517 // is useful to check the correcteness of the sequence due to semantic
2518 // change of bitcast.
2519 assert(
2520 SrcTy->isPtrOrPtrVectorTy() &&
2521 MidTy->isPtrOrPtrVectorTy() &&
2522 DstTy->isPtrOrPtrVectorTy() &&
2523 SrcTy->getPointerAddressSpace() != MidTy->getPointerAddressSpace() &&
2524 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2525 "Illegal addrspacecast, bitcast sequence!");
2526 // Allowed, use first cast's opcode
2527 return firstOp;
2528 case 14:
Jingyue Wu77145d92014-06-06 21:52:55 +00002529 // bitcast, addrspacecast -> addrspacecast if the element type of
2530 // bitcast's source is the same as that of addrspacecast's destination.
2531 if (SrcTy->getPointerElementType() == DstTy->getPointerElementType())
2532 return Instruction::AddrSpaceCast;
2533 return 0;
2534
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002535 case 15:
2536 // FIXME: this state can be merged with (1), but the following assert
2537 // is useful to check the correcteness of the sequence due to semantic
2538 // change of bitcast.
2539 assert(
2540 SrcTy->isIntOrIntVectorTy() &&
2541 MidTy->isPtrOrPtrVectorTy() &&
2542 DstTy->isPtrOrPtrVectorTy() &&
2543 MidTy->getPointerAddressSpace() == DstTy->getPointerAddressSpace() &&
2544 "Illegal inttoptr, bitcast sequence!");
2545 // Allowed, use first cast's opcode
2546 return firstOp;
2547 case 16:
2548 // FIXME: this state can be merged with (2), but the following assert
2549 // is useful to check the correcteness of the sequence due to semantic
2550 // change of bitcast.
2551 assert(
2552 SrcTy->isPtrOrPtrVectorTy() &&
2553 MidTy->isPtrOrPtrVectorTy() &&
2554 DstTy->isIntOrIntVectorTy() &&
2555 SrcTy->getPointerAddressSpace() == MidTy->getPointerAddressSpace() &&
2556 "Illegal bitcast, ptrtoint sequence!");
2557 // Allowed, use second cast's opcode
2558 return secondOp;
Fiona Glaser0d41db12015-04-21 00:05:41 +00002559 case 17:
2560 // (sitofp (zext x)) -> (uitofp x)
2561 return Instruction::UIToFP;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002562 case 99:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002563 // Cast combination can't happen (error in input). This is for all cases
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002564 // where the MidTy is not the same for the two cast instructions.
Craig Topperc514b542012-02-05 22:14:15 +00002565 llvm_unreachable("Invalid Cast Combination");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002566 default:
Craig Topperc514b542012-02-05 22:14:15 +00002567 llvm_unreachable("Error in CastResults table!!!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002568 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002569}
2570
Chris Lattner229907c2011-07-18 04:54:35 +00002571CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002572 const Twine &Name, Instruction *InsertBefore) {
Duncan Sands7f646562011-05-18 09:21:57 +00002573 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002574 // Construct and return the appropriate CastInst subclass
2575 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002576 case Trunc: return new TruncInst (S, Ty, Name, InsertBefore);
2577 case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore);
2578 case SExt: return new SExtInst (S, Ty, Name, InsertBefore);
2579 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore);
2580 case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore);
2581 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore);
2582 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore);
2583 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore);
2584 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore);
2585 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore);
2586 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore);
2587 case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore);
2588 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertBefore);
2589 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002590 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002591}
2592
Chris Lattner229907c2011-07-18 04:54:35 +00002593CastInst *CastInst::Create(Instruction::CastOps op, Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002594 const Twine &Name, BasicBlock *InsertAtEnd) {
Duncan Sands7f646562011-05-18 09:21:57 +00002595 assert(castIsValid(op, S, Ty) && "Invalid cast!");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002596 // Construct and return the appropriate CastInst subclass
2597 switch (op) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002598 case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd);
2599 case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd);
2600 case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd);
2601 case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd);
2602 case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd);
2603 case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd);
2604 case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd);
2605 case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd);
2606 case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd);
2607 case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd);
2608 case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd);
2609 case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd);
2610 case AddrSpaceCast: return new AddrSpaceCastInst (S, Ty, Name, InsertAtEnd);
2611 default: llvm_unreachable("Invalid opcode provided");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002612 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002613}
2614
Chris Lattner229907c2011-07-18 04:54:35 +00002615CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002616 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002617 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002618 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002619 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2620 return Create(Instruction::ZExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002621}
2622
Chris Lattner229907c2011-07-18 04:54:35 +00002623CastInst *CastInst::CreateZExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002624 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002625 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002626 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002627 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2628 return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002629}
2630
Chris Lattner229907c2011-07-18 04:54:35 +00002631CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002632 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002633 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002634 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002635 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2636 return Create(Instruction::SExt, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002637}
2638
Chris Lattner229907c2011-07-18 04:54:35 +00002639CastInst *CastInst::CreateSExtOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002640 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002641 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002642 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002643 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2644 return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002645}
2646
Chris Lattner229907c2011-07-18 04:54:35 +00002647CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002648 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002649 Instruction *InsertBefore) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002650 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002651 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2652 return Create(Instruction::Trunc, S, Ty, Name, InsertBefore);
Reid Spencer5c140882006-12-04 20:17:56 +00002653}
2654
Chris Lattner229907c2011-07-18 04:54:35 +00002655CastInst *CastInst::CreateTruncOrBitCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002656 const Twine &Name,
Reid Spencer5c140882006-12-04 20:17:56 +00002657 BasicBlock *InsertAtEnd) {
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002658 if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002659 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2660 return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd);
Reid Spencer5c140882006-12-04 20:17:56 +00002661}
2662
Chris Lattner229907c2011-07-18 04:54:35 +00002663CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002664 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002665 BasicBlock *InsertAtEnd) {
Matt Arsenault065ced92013-07-31 00:17:33 +00002666 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2667 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
2668 "Invalid cast");
2669 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002670 assert((!Ty->isVectorTy() ||
2671 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002672 "Invalid cast");
2673
Matt Arsenault065ced92013-07-31 00:17:33 +00002674 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002675 return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002676
Matt Arsenault740980e2014-07-14 17:24:35 +00002677 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertAtEnd);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002678}
2679
2680/// @brief Create a BitCast or a PtrToInt cast instruction
Matt Arsenault065ced92013-07-31 00:17:33 +00002681CastInst *CastInst::CreatePointerCast(Value *S, Type *Ty,
2682 const Twine &Name,
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002683 Instruction *InsertBefore) {
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002684 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2685 assert((Ty->isIntOrIntVectorTy() || Ty->isPtrOrPtrVectorTy()) &&
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002686 "Invalid cast");
Matt Arsenault065ced92013-07-31 00:17:33 +00002687 assert(Ty->isVectorTy() == S->getType()->isVectorTy() && "Invalid cast");
Richard Trieu8dc43232013-07-31 04:07:28 +00002688 assert((!Ty->isVectorTy() ||
2689 Ty->getVectorNumElements() == S->getType()->getVectorNumElements()) &&
Matt Arsenault065ced92013-07-31 00:17:33 +00002690 "Invalid cast");
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002691
Evgeniy Stepanovc9bd35b2013-01-15 16:43:00 +00002692 if (Ty->isIntOrIntVectorTy())
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002693 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002694
Matt Arsenault740980e2014-07-14 17:24:35 +00002695 return CreatePointerBitCastOrAddrSpaceCast(S, Ty, Name, InsertBefore);
2696}
2697
2698CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2699 Value *S, Type *Ty,
2700 const Twine &Name,
2701 BasicBlock *InsertAtEnd) {
2702 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2703 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2704
2705 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
2706 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertAtEnd);
2707
2708 return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd);
2709}
2710
2711CastInst *CastInst::CreatePointerBitCastOrAddrSpaceCast(
2712 Value *S, Type *Ty,
2713 const Twine &Name,
2714 Instruction *InsertBefore) {
2715 assert(S->getType()->isPtrOrPtrVectorTy() && "Invalid cast");
2716 assert(Ty->isPtrOrPtrVectorTy() && "Invalid cast");
2717
2718 if (S->getType()->getPointerAddressSpace() != Ty->getPointerAddressSpace())
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002719 return Create(Instruction::AddrSpaceCast, S, Ty, Name, InsertBefore);
2720
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002721 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
Reid Spencerd5a3f0d2006-12-05 03:28:26 +00002722}
2723
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002724CastInst *CastInst::CreateBitOrPointerCast(Value *S, Type *Ty,
2725 const Twine &Name,
2726 Instruction *InsertBefore) {
2727 if (S->getType()->isPointerTy() && Ty->isIntegerTy())
2728 return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore);
2729 if (S->getType()->isIntegerTy() && Ty->isPointerTy())
2730 return Create(Instruction::IntToPtr, S, Ty, Name, InsertBefore);
2731
2732 return Create(Instruction::BitCast, S, Ty, Name, InsertBefore);
2733}
2734
Matt Arsenault740980e2014-07-14 17:24:35 +00002735CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002736 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002737 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002738 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Chris Lattner5370ae72010-01-10 20:21:42 +00002739 "Invalid integer cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002740 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2741 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002742 Instruction::CastOps opcode =
2743 (SrcBits == DstBits ? Instruction::BitCast :
2744 (SrcBits > DstBits ? Instruction::Trunc :
2745 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002746 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002747}
2748
Chris Lattner229907c2011-07-18 04:54:35 +00002749CastInst *CastInst::CreateIntegerCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002750 bool isSigned, const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002751 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002752 assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() &&
Dan Gohman7889f2b2009-06-15 22:25:12 +00002753 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002754 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2755 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002756 Instruction::CastOps opcode =
2757 (SrcBits == DstBits ? Instruction::BitCast :
2758 (SrcBits > DstBits ? Instruction::Trunc :
2759 (isSigned ? Instruction::SExt : Instruction::ZExt)));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002760 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002761}
2762
Chris Lattner229907c2011-07-18 04:54:35 +00002763CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002764 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002765 Instruction *InsertBefore) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002766 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002767 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002768 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2769 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002770 Instruction::CastOps opcode =
2771 (SrcBits == DstBits ? Instruction::BitCast :
2772 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002773 return Create(opcode, C, Ty, Name, InsertBefore);
Reid Spencer7e933472006-12-12 00:49:44 +00002774}
2775
Chris Lattner229907c2011-07-18 04:54:35 +00002776CastInst *CastInst::CreateFPCast(Value *C, Type *Ty,
Daniel Dunbar4975db62009-07-25 04:41:11 +00002777 const Twine &Name,
Reid Spencer7e933472006-12-12 00:49:44 +00002778 BasicBlock *InsertAtEnd) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002779 assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() &&
Reid Spencer7e933472006-12-12 00:49:44 +00002780 "Invalid cast");
Dan Gohman7ccc52f2009-06-15 22:12:54 +00002781 unsigned SrcBits = C->getType()->getScalarSizeInBits();
2782 unsigned DstBits = Ty->getScalarSizeInBits();
Reid Spencer7e933472006-12-12 00:49:44 +00002783 Instruction::CastOps opcode =
2784 (SrcBits == DstBits ? Instruction::BitCast :
2785 (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt));
Gabor Greife1f6e4b2008-05-16 19:29:10 +00002786 return Create(opcode, C, Ty, Name, InsertAtEnd);
Reid Spencer7e933472006-12-12 00:49:44 +00002787}
2788
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002789// Check whether it is valid to call getCastOpcode for these types.
2790// This routine must be kept in sync with getCastOpcode.
2791bool CastInst::isCastable(Type *SrcTy, Type *DestTy) {
2792 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2793 return false;
2794
2795 if (SrcTy == DestTy)
2796 return true;
2797
2798 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2799 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
2800 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2801 // An element by element cast. Valid if casting the elements is valid.
2802 SrcTy = SrcVecTy->getElementType();
2803 DestTy = DestVecTy->getElementType();
2804 }
2805
2806 // Get the bit sizes, we'll need these
2807 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2808 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2809
2810 // Run through the possibilities ...
2811 if (DestTy->isIntegerTy()) { // Casting to integral
David Blaikie9965c5a2015-03-23 19:51:23 +00002812 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002813 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002814 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002815 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002816 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002817 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002818 // Casting from something else
2819 return SrcTy->isPointerTy();
2820 }
2821 if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2822 if (SrcTy->isIntegerTy()) // Casting from integral
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002823 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002824 if (SrcTy->isFloatingPointTy()) // Casting from floating pt
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002825 return true;
David Blaikie9965c5a2015-03-23 19:51:23 +00002826 if (SrcTy->isVectorTy()) // Casting from vector
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002827 return DestBits == SrcBits;
David Blaikie9965c5a2015-03-23 19:51:23 +00002828 // Casting from something else
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002829 return false;
2830 }
David Blaikie9965c5a2015-03-23 19:51:23 +00002831 if (DestTy->isVectorTy()) // Casting to vector
2832 return DestBits == SrcBits;
2833 if (DestTy->isPointerTy()) { // Casting to pointer
2834 if (SrcTy->isPointerTy()) // Casting from pointer
2835 return true;
2836 return SrcTy->isIntegerTy(); // Casting from integral
2837 }
2838 if (DestTy->isX86_MMXTy()) {
2839 if (SrcTy->isVectorTy())
2840 return DestBits == SrcBits; // 64-bit vector to MMX
2841 return false;
2842 } // Casting to something else
2843 return false;
Matt Arsenaultb4019ae2013-07-30 22:02:14 +00002844}
2845
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002846bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) {
2847 if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType())
2848 return false;
2849
2850 if (SrcTy == DestTy)
2851 return true;
2852
2853 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
2854 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy)) {
2855 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2856 // An element by element cast. Valid if casting the elements is valid.
2857 SrcTy = SrcVecTy->getElementType();
2858 DestTy = DestVecTy->getElementType();
2859 }
2860 }
2861 }
2862
2863 if (PointerType *DestPtrTy = dyn_cast<PointerType>(DestTy)) {
2864 if (PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy)) {
2865 return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace();
2866 }
2867 }
2868
2869 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2870 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
2871
2872 // Could still have vectors of pointers if the number of elements doesn't
2873 // match
2874 if (SrcBits == 0 || DestBits == 0)
2875 return false;
2876
2877 if (SrcBits != DestBits)
2878 return false;
2879
2880 if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy())
2881 return false;
2882
2883 return true;
2884}
2885
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002886bool CastInst::isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy,
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002887 const DataLayout &DL) {
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002888 if (auto *PtrTy = dyn_cast<PointerType>(SrcTy))
2889 if (auto *IntTy = dyn_cast<IntegerType>(DestTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002890 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002891 if (auto *PtrTy = dyn_cast<PointerType>(DestTy))
2892 if (auto *IntTy = dyn_cast<IntegerType>(SrcTy))
Mehdi Aminia28d91d2015-03-10 02:37:25 +00002893 return IntTy->getBitWidth() == DL.getPointerTypeSizeInBits(PtrTy);
Chandler Carruth1a3c2c42014-11-25 08:20:27 +00002894
2895 return isBitCastable(SrcTy, DestTy);
2896}
2897
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002898// Provide a way to get a "cast" where the cast opcode is inferred from the
2899// types and size of the operand. This, basically, is a parallel of the
Reid Spencer00e5e0e2007-01-17 02:46:11 +00002900// logic in the castIsValid function below. This axiom should hold:
2901// castIsValid( getCastOpcode(Val, Ty), Val, Ty)
2902// should not assert in castIsValid. In other words, this produces a "correct"
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002903// casting opcode for the arguments passed to it.
Duncan Sands55e50902008-01-06 10:12:28 +00002904// This routine must be kept in sync with isCastable.
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002905Instruction::CastOps
Reid Spencerc4dacf22006-12-04 02:43:42 +00002906CastInst::getCastOpcode(
Chris Lattner229907c2011-07-18 04:54:35 +00002907 const Value *Src, bool SrcIsSigned, Type *DestTy, bool DestIsSigned) {
2908 Type *SrcTy = Src->getType();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002909
Duncan Sands55e50902008-01-06 10:12:28 +00002910 assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() &&
2911 "Only first class types are castable!");
2912
Duncan Sandsa8514532011-05-18 07:13:41 +00002913 if (SrcTy == DestTy)
2914 return BitCast;
2915
Matt Arsenaultcacbb232013-07-30 20:45:05 +00002916 // FIXME: Check address space sizes here
Chris Lattner229907c2011-07-18 04:54:35 +00002917 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy))
2918 if (VectorType *DestVecTy = dyn_cast<VectorType>(DestTy))
Duncan Sandsa8514532011-05-18 07:13:41 +00002919 if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) {
2920 // An element by element cast. Find the appropriate opcode based on the
2921 // element types.
2922 SrcTy = SrcVecTy->getElementType();
2923 DestTy = DestVecTy->getElementType();
2924 }
2925
2926 // Get the bit sizes, we'll need these
Duncan Sands7f646562011-05-18 09:21:57 +00002927 unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr
2928 unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr
Duncan Sandsa8514532011-05-18 07:13:41 +00002929
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002930 // Run through the possibilities ...
Duncan Sands9dff9be2010-02-15 16:12:20 +00002931 if (DestTy->isIntegerTy()) { // Casting to integral
2932 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002933 if (DestBits < SrcBits)
2934 return Trunc; // int -> smaller int
2935 else if (DestBits > SrcBits) { // its an extension
Reid Spencerc4dacf22006-12-04 02:43:42 +00002936 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002937 return SExt; // signed -> SEXT
2938 else
2939 return ZExt; // unsigned -> ZEXT
2940 } else {
2941 return BitCast; // Same size, No-op cast
2942 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002943 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencerc4dacf22006-12-04 02:43:42 +00002944 if (DestIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002945 return FPToSI; // FP -> sint
2946 else
2947 return FPToUI; // FP -> uint
Duncan Sands27bd0df2011-05-18 10:59:25 +00002948 } else if (SrcTy->isVectorTy()) {
2949 assert(DestBits == SrcBits &&
2950 "Casting vector to integer of different width");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002951 return BitCast; // Same size, no-op cast
2952 } else {
Duncan Sands19d0b472010-02-16 11:11:14 +00002953 assert(SrcTy->isPointerTy() &&
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002954 "Casting from a value that is not first-class type");
2955 return PtrToInt; // ptr -> int
2956 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00002957 } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt
2958 if (SrcTy->isIntegerTy()) { // Casting from integral
Reid Spencerc4dacf22006-12-04 02:43:42 +00002959 if (SrcIsSigned)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002960 return SIToFP; // sint -> FP
2961 else
2962 return UIToFP; // uint -> FP
Duncan Sands9dff9be2010-02-15 16:12:20 +00002963 } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002964 if (DestBits < SrcBits) {
2965 return FPTrunc; // FP -> smaller FP
2966 } else if (DestBits > SrcBits) {
2967 return FPExt; // FP -> larger FP
2968 } else {
2969 return BitCast; // same size, no-op cast
2970 }
Duncan Sands27bd0df2011-05-18 10:59:25 +00002971 } else if (SrcTy->isVectorTy()) {
2972 assert(DestBits == SrcBits &&
Dan Gohmanfead7972007-05-11 21:43:24 +00002973 "Casting vector to floating point of different width");
Devang Patele9432132008-11-05 01:37:40 +00002974 return BitCast; // same size, no-op cast
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002975 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002976 llvm_unreachable("Casting pointer or non-first class to float");
Duncan Sands27bd0df2011-05-18 10:59:25 +00002977 } else if (DestTy->isVectorTy()) {
2978 assert(DestBits == SrcBits &&
2979 "Illegal cast to vector (wrong type or size)");
2980 return BitCast;
Duncan Sands19d0b472010-02-16 11:11:14 +00002981 } else if (DestTy->isPointerTy()) {
2982 if (SrcTy->isPointerTy()) {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002983 if (DestTy->getPointerAddressSpace() != SrcTy->getPointerAddressSpace())
2984 return AddrSpaceCast;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002985 return BitCast; // ptr -> ptr
Duncan Sands9dff9be2010-02-15 16:12:20 +00002986 } else if (SrcTy->isIntegerTy()) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002987 return IntToPtr; // int -> ptr
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002988 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002989 llvm_unreachable("Casting pointer to other than pointer or int");
Dale Johannesendd224d22010-09-30 23:57:10 +00002990 } else if (DestTy->isX86_MMXTy()) {
Duncan Sands27bd0df2011-05-18 10:59:25 +00002991 if (SrcTy->isVectorTy()) {
2992 assert(DestBits == SrcBits && "Casting vector of wrong width to X86_MMX");
Dale Johannesendd224d22010-09-30 23:57:10 +00002993 return BitCast; // 64-bit vector to MMX
Dale Johannesendd224d22010-09-30 23:57:10 +00002994 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002995 llvm_unreachable("Illegal cast to X86_MMX");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002996 }
Ahmed Charles636a3d62012-02-19 11:37:01 +00002997 llvm_unreachable("Casting to type that is not first-class");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00002998}
2999
3000//===----------------------------------------------------------------------===//
3001// CastInst SubClass Constructors
3002//===----------------------------------------------------------------------===//
3003
3004/// Check that the construction parameters for a CastInst are correct. This
3005/// could be broken out into the separate constructors but it is useful to have
3006/// it in one place and to eliminate the redundant code for getting the sizes
3007/// of the types involved.
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003008bool
Chris Lattner229907c2011-07-18 04:54:35 +00003009CastInst::castIsValid(Instruction::CastOps op, Value *S, Type *DstTy) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003010
3011 // Check for type sanity on the arguments
Chris Lattner229907c2011-07-18 04:54:35 +00003012 Type *SrcTy = S->getType();
Evan Cheng098d7b72013-01-10 23:22:53 +00003013
Chris Lattner37bc78a2010-01-26 21:51:43 +00003014 if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() ||
3015 SrcTy->isAggregateType() || DstTy->isAggregateType())
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003016 return false;
3017
3018 // Get the size of the types in bits, we'll need this later
Dan Gohman7ccc52f2009-06-15 22:12:54 +00003019 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
3020 unsigned DstBitSize = DstTy->getScalarSizeInBits();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003021
Duncan Sands7f646562011-05-18 09:21:57 +00003022 // If these are vector types, get the lengths of the vectors (using zero for
3023 // scalar types means that checking that vector lengths match also checks that
3024 // scalars are not being converted to vectors or vectors to scalars).
3025 unsigned SrcLength = SrcTy->isVectorTy() ?
3026 cast<VectorType>(SrcTy)->getNumElements() : 0;
3027 unsigned DstLength = DstTy->isVectorTy() ?
3028 cast<VectorType>(DstTy)->getNumElements() : 0;
3029
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003030 // Switch on the opcode provided
3031 switch (op) {
3032 default: return false; // This is an input error
3033 case Instruction::Trunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003034 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3035 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003036 case Instruction::ZExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003037 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3038 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003039 case Instruction::SExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003040 return SrcTy->isIntOrIntVectorTy() && DstTy->isIntOrIntVectorTy() &&
3041 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003042 case Instruction::FPTrunc:
Duncan Sands7f646562011-05-18 09:21:57 +00003043 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3044 SrcLength == DstLength && SrcBitSize > DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003045 case Instruction::FPExt:
Duncan Sands7f646562011-05-18 09:21:57 +00003046 return SrcTy->isFPOrFPVectorTy() && DstTy->isFPOrFPVectorTy() &&
3047 SrcLength == DstLength && SrcBitSize < DstBitSize;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003048 case Instruction::UIToFP:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003049 case Instruction::SIToFP:
Duncan Sands7f646562011-05-18 09:21:57 +00003050 return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy() &&
3051 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003052 case Instruction::FPToUI:
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003053 case Instruction::FPToSI:
Duncan Sands7f646562011-05-18 09:21:57 +00003054 return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy() &&
3055 SrcLength == DstLength;
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003056 case Instruction::PtrToInt:
Chris Lattner8a3df542012-01-25 01:32:59 +00003057 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003058 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003059 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3060 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3061 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003062 return SrcTy->getScalarType()->isPointerTy() &&
3063 DstTy->getScalarType()->isIntegerTy();
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003064 case Instruction::IntToPtr:
Chris Lattner8a3df542012-01-25 01:32:59 +00003065 if (isa<VectorType>(SrcTy) != isa<VectorType>(DstTy))
Nadav Rotem3924cb02011-12-05 06:29:09 +00003066 return false;
Chris Lattner8a3df542012-01-25 01:32:59 +00003067 if (VectorType *VT = dyn_cast<VectorType>(SrcTy))
3068 if (VT->getNumElements() != cast<VectorType>(DstTy)->getNumElements())
3069 return false;
Nadav Rotem3924cb02011-12-05 06:29:09 +00003070 return SrcTy->getScalarType()->isIntegerTy() &&
3071 DstTy->getScalarType()->isPointerTy();
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003072 case Instruction::BitCast: {
3073 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3074 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3075
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003076 // BitCast implies a no-op cast of type only. No bits change.
3077 // However, you can't cast pointers to anything but pointers.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003078 if (!SrcPtrTy != !DstPtrTy)
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003079 return false;
3080
Alp Tokerf907b892013-12-05 05:44:44 +00003081 // For non-pointer cases, the cast is okay if the source and destination bit
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003082 // widths are identical.
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003083 if (!SrcPtrTy)
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003084 return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits();
3085
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003086 // If both are pointers then the address spaces must match.
3087 if (SrcPtrTy->getAddressSpace() != DstPtrTy->getAddressSpace())
3088 return false;
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003089
Matt Arsenaultfc3c91d2014-01-22 19:21:33 +00003090 // A vector of pointers must have the same number of elements.
3091 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3092 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3093 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3094
3095 return false;
3096 }
3097
3098 return true;
3099 }
3100 case Instruction::AddrSpaceCast: {
3101 PointerType *SrcPtrTy = dyn_cast<PointerType>(SrcTy->getScalarType());
3102 if (!SrcPtrTy)
3103 return false;
3104
3105 PointerType *DstPtrTy = dyn_cast<PointerType>(DstTy->getScalarType());
3106 if (!DstPtrTy)
3107 return false;
3108
3109 if (SrcPtrTy->getAddressSpace() == DstPtrTy->getAddressSpace())
3110 return false;
3111
3112 if (VectorType *SrcVecTy = dyn_cast<VectorType>(SrcTy)) {
3113 if (VectorType *DstVecTy = dyn_cast<VectorType>(DstTy))
3114 return (SrcVecTy->getNumElements() == DstVecTy->getNumElements());
3115
3116 return false;
3117 }
3118
3119 return true;
3120 }
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003121 }
3122}
3123
3124TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003125 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003126) : CastInst(Ty, Trunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003127 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003128}
3129
3130TruncInst::TruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003131 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003132) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003133 assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003134}
3135
3136ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003137 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003138) : CastInst(Ty, ZExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003139 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003140}
3141
3142ZExtInst::ZExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003143 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003144) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003145 assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003146}
3147SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003148 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003149) : CastInst(Ty, SExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003150 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003151}
3152
Jeff Cohencc08c832006-12-02 02:22:01 +00003153SExtInst::SExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003154 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003155) : CastInst(Ty, SExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003156 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003157}
3158
3159FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003160 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003161) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003162 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003163}
3164
3165FPTruncInst::FPTruncInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003166 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003167) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003168 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003169}
3170
3171FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003172 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003173) : CastInst(Ty, FPExt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003174 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003175}
3176
3177FPExtInst::FPExtInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003178 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003179) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003180 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003181}
3182
3183UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003184 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003185) : CastInst(Ty, UIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003186 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003187}
3188
3189UIToFPInst::UIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003190 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003191) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003192 assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003193}
3194
3195SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003196 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003197) : CastInst(Ty, SIToFP, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003198 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003199}
3200
3201SIToFPInst::SIToFPInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003202 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003203) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003204 assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003205}
3206
3207FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003208 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003209) : CastInst(Ty, FPToUI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003210 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003211}
3212
3213FPToUIInst::FPToUIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003214 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003215) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003216 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003217}
3218
3219FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003220 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003221) : CastInst(Ty, FPToSI, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003222 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003223}
3224
3225FPToSIInst::FPToSIInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003226 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003227) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003228 assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003229}
3230
3231PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003232 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003233) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003234 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003235}
3236
3237PtrToIntInst::PtrToIntInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003238 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003239) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003240 assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003241}
3242
3243IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003244 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003245) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003246 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003247}
3248
3249IntToPtrInst::IntToPtrInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003250 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003251) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003252 assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003253}
3254
3255BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003256 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003257) : CastInst(Ty, BitCast, S, Name, InsertBefore) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003258 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003259}
3260
3261BitCastInst::BitCastInst(
Chris Lattner229907c2011-07-18 04:54:35 +00003262 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003263) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) {
Reid Spencer00e5e0e2007-01-17 02:46:11 +00003264 assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast");
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003265}
Chris Lattnerf16dc002006-09-17 19:29:56 +00003266
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003267AddrSpaceCastInst::AddrSpaceCastInst(
3268 Value *S, Type *Ty, const Twine &Name, Instruction *InsertBefore
3269) : CastInst(Ty, AddrSpaceCast, S, Name, InsertBefore) {
3270 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3271}
3272
3273AddrSpaceCastInst::AddrSpaceCastInst(
3274 Value *S, Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd
3275) : CastInst(Ty, AddrSpaceCast, S, Name, InsertAtEnd) {
3276 assert(castIsValid(getOpcode(), S, Ty) && "Illegal AddrSpaceCast");
3277}
3278
Chris Lattnerf16dc002006-09-17 19:29:56 +00003279//===----------------------------------------------------------------------===//
Reid Spencerd9436b62006-11-20 01:22:35 +00003280// CmpInst Classes
3281//===----------------------------------------------------------------------===//
3282
Craig Topper3186c012012-09-23 02:12:10 +00003283void CmpInst::anchor() {}
Chris Lattneraec33da2010-01-22 06:25:37 +00003284
Craig Topper1c3f2832015-12-15 06:11:33 +00003285CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3286 Value *RHS, const Twine &Name, Instruction *InsertBefore)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003287 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003288 OperandTraits<CmpInst>::op_begin(this),
3289 OperandTraits<CmpInst>::operands(this),
3290 InsertBefore) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003291 Op<0>() = LHS;
3292 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003293 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003294 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003295}
Gabor Greiff6caff662008-05-10 08:32:32 +00003296
Craig Topper1c3f2832015-12-15 06:11:33 +00003297CmpInst::CmpInst(Type *ty, OtherOps op, Predicate predicate, Value *LHS,
3298 Value *RHS, const Twine &Name, BasicBlock *InsertAtEnd)
Nate Begeman66d0a0e2008-05-12 20:11:05 +00003299 : Instruction(ty, op,
Gabor Greiff6caff662008-05-10 08:32:32 +00003300 OperandTraits<CmpInst>::op_begin(this),
3301 OperandTraits<CmpInst>::operands(this),
3302 InsertAtEnd) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003303 Op<0>() = LHS;
3304 Op<1>() = RHS;
Chris Lattnerb9c86512009-12-29 02:14:09 +00003305 setPredicate((Predicate)predicate);
Reid Spencer871a9ea2007-04-11 13:04:48 +00003306 setName(Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003307}
3308
3309CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003310CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003311 const Twine &Name, Instruction *InsertBefore) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003312 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003313 if (InsertBefore)
3314 return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate),
3315 S1, S2, Name);
3316 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003317 return new ICmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003318 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003319 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003320
3321 if (InsertBefore)
3322 return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate),
3323 S1, S2, Name);
3324 else
Dan Gohmanad1f0a12009-08-25 23:17:54 +00003325 return new FCmpInst(CmpInst::Predicate(predicate),
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003326 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003327}
3328
3329CmpInst *
Craig Topper1c3f2832015-12-15 06:11:33 +00003330CmpInst::Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2,
Daniel Dunbar4975db62009-07-25 04:41:11 +00003331 const Twine &Name, BasicBlock *InsertAtEnd) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003332 if (Op == Instruction::ICmp) {
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003333 return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3334 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003335 }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003336 return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate),
3337 S1, S2, Name);
Reid Spencerd9436b62006-11-20 01:22:35 +00003338}
3339
3340void CmpInst::swapOperands() {
3341 if (ICmpInst *IC = dyn_cast<ICmpInst>(this))
3342 IC->swapOperands();
3343 else
3344 cast<FCmpInst>(this)->swapOperands();
3345}
3346
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003347bool CmpInst::isCommutative() const {
3348 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003349 return IC->isCommutative();
3350 return cast<FCmpInst>(this)->isCommutative();
3351}
3352
Duncan Sands95c4ecc2011-01-04 12:52:29 +00003353bool CmpInst::isEquality() const {
3354 if (const ICmpInst *IC = dyn_cast<ICmpInst>(this))
Reid Spencerd9436b62006-11-20 01:22:35 +00003355 return IC->isEquality();
3356 return cast<FCmpInst>(this)->isEquality();
3357}
3358
3359
Dan Gohman4e724382008-05-31 02:47:54 +00003360CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003361 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003362 default: llvm_unreachable("Unknown cmp predicate!");
Reid Spencerd9436b62006-11-20 01:22:35 +00003363 case ICMP_EQ: return ICMP_NE;
3364 case ICMP_NE: return ICMP_EQ;
3365 case ICMP_UGT: return ICMP_ULE;
3366 case ICMP_ULT: return ICMP_UGE;
3367 case ICMP_UGE: return ICMP_ULT;
3368 case ICMP_ULE: return ICMP_UGT;
3369 case ICMP_SGT: return ICMP_SLE;
3370 case ICMP_SLT: return ICMP_SGE;
3371 case ICMP_SGE: return ICMP_SLT;
3372 case ICMP_SLE: return ICMP_SGT;
Reid Spencerd9436b62006-11-20 01:22:35 +00003373
Dan Gohman4e724382008-05-31 02:47:54 +00003374 case FCMP_OEQ: return FCMP_UNE;
3375 case FCMP_ONE: return FCMP_UEQ;
3376 case FCMP_OGT: return FCMP_ULE;
3377 case FCMP_OLT: return FCMP_UGE;
3378 case FCMP_OGE: return FCMP_ULT;
3379 case FCMP_OLE: return FCMP_UGT;
3380 case FCMP_UEQ: return FCMP_ONE;
3381 case FCMP_UNE: return FCMP_OEQ;
3382 case FCMP_UGT: return FCMP_OLE;
3383 case FCMP_ULT: return FCMP_OGE;
3384 case FCMP_UGE: return FCMP_OLT;
3385 case FCMP_ULE: return FCMP_OGT;
3386 case FCMP_ORD: return FCMP_UNO;
3387 case FCMP_UNO: return FCMP_ORD;
3388 case FCMP_TRUE: return FCMP_FALSE;
3389 case FCMP_FALSE: return FCMP_TRUE;
Reid Spencerd9436b62006-11-20 01:22:35 +00003390 }
3391}
3392
Pete Cooper1d078692015-12-14 20:29:16 +00003393void ICmpInst::anchor() {}
3394
Reid Spencer266e42b2006-12-23 06:05:41 +00003395ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) {
3396 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003397 default: llvm_unreachable("Unknown icmp predicate!");
Reid Spencer266e42b2006-12-23 06:05:41 +00003398 case ICMP_EQ: case ICMP_NE:
3399 case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE:
3400 return pred;
3401 case ICMP_UGT: return ICMP_SGT;
3402 case ICMP_ULT: return ICMP_SLT;
3403 case ICMP_UGE: return ICMP_SGE;
3404 case ICMP_ULE: return ICMP_SLE;
3405 }
3406}
3407
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003408ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) {
3409 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003410 default: llvm_unreachable("Unknown icmp predicate!");
Nick Lewycky8ea81e82008-01-28 03:48:02 +00003411 case ICMP_EQ: case ICMP_NE:
3412 case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE:
3413 return pred;
3414 case ICMP_SGT: return ICMP_UGT;
3415 case ICMP_SLT: return ICMP_ULT;
3416 case ICMP_SGE: return ICMP_UGE;
3417 case ICMP_SLE: return ICMP_ULE;
3418 }
3419}
3420
Reid Spencer0286bc12007-02-28 22:00:54 +00003421/// Initialize a set of values that all satisfy the condition with C.
3422///
3423ConstantRange
3424ICmpInst::makeConstantRange(Predicate pred, const APInt &C) {
3425 APInt Lower(C);
3426 APInt Upper(C);
3427 uint32_t BitWidth = C.getBitWidth();
3428 switch (pred) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00003429 default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!");
Jakub Staszak773be0c2013-03-20 23:56:19 +00003430 case ICmpInst::ICMP_EQ: ++Upper; break;
3431 case ICmpInst::ICMP_NE: ++Lower; break;
Dan Gohmand86e2952010-01-26 16:04:20 +00003432 case ICmpInst::ICMP_ULT:
3433 Lower = APInt::getMinValue(BitWidth);
3434 // Check for an empty-set condition.
3435 if (Lower == Upper)
3436 return ConstantRange(BitWidth, /*isFullSet=*/false);
3437 break;
3438 case ICmpInst::ICMP_SLT:
3439 Lower = APInt::getSignedMinValue(BitWidth);
3440 // Check for an empty-set condition.
3441 if (Lower == Upper)
3442 return ConstantRange(BitWidth, /*isFullSet=*/false);
3443 break;
Reid Spencer0286bc12007-02-28 22:00:54 +00003444 case ICmpInst::ICMP_UGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003445 ++Lower; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003446 // Check for an empty-set condition.
3447 if (Lower == Upper)
3448 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003449 break;
3450 case ICmpInst::ICMP_SGT:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003451 ++Lower; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003452 // Check for an empty-set condition.
3453 if (Lower == Upper)
3454 return ConstantRange(BitWidth, /*isFullSet=*/false);
Reid Spencer0286bc12007-02-28 22:00:54 +00003455 break;
3456 case ICmpInst::ICMP_ULE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003457 Lower = APInt::getMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003458 // Check for a full-set condition.
3459 if (Lower == Upper)
3460 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003461 break;
3462 case ICmpInst::ICMP_SLE:
Jakub Staszak773be0c2013-03-20 23:56:19 +00003463 Lower = APInt::getSignedMinValue(BitWidth); ++Upper;
Dan Gohmand86e2952010-01-26 16:04:20 +00003464 // Check for a full-set condition.
3465 if (Lower == Upper)
3466 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003467 break;
3468 case ICmpInst::ICMP_UGE:
3469 Upper = APInt::getMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003470 // Check for a full-set condition.
3471 if (Lower == Upper)
3472 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003473 break;
3474 case ICmpInst::ICMP_SGE:
3475 Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max)
Dan Gohmand86e2952010-01-26 16:04:20 +00003476 // Check for a full-set condition.
3477 if (Lower == Upper)
3478 return ConstantRange(BitWidth, /*isFullSet=*/true);
Reid Spencer0286bc12007-02-28 22:00:54 +00003479 break;
3480 }
3481 return ConstantRange(Lower, Upper);
3482}
3483
Dan Gohman4e724382008-05-31 02:47:54 +00003484CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) {
Reid Spencerd9436b62006-11-20 01:22:35 +00003485 switch (pred) {
Craig Topperc514b542012-02-05 22:14:15 +00003486 default: llvm_unreachable("Unknown cmp predicate!");
Dan Gohman4e724382008-05-31 02:47:54 +00003487 case ICMP_EQ: case ICMP_NE:
3488 return pred;
3489 case ICMP_SGT: return ICMP_SLT;
3490 case ICMP_SLT: return ICMP_SGT;
3491 case ICMP_SGE: return ICMP_SLE;
3492 case ICMP_SLE: return ICMP_SGE;
3493 case ICMP_UGT: return ICMP_ULT;
3494 case ICMP_ULT: return ICMP_UGT;
3495 case ICMP_UGE: return ICMP_ULE;
3496 case ICMP_ULE: return ICMP_UGE;
3497
Reid Spencerd9436b62006-11-20 01:22:35 +00003498 case FCMP_FALSE: case FCMP_TRUE:
3499 case FCMP_OEQ: case FCMP_ONE:
3500 case FCMP_UEQ: case FCMP_UNE:
3501 case FCMP_ORD: case FCMP_UNO:
3502 return pred;
3503 case FCMP_OGT: return FCMP_OLT;
3504 case FCMP_OLT: return FCMP_OGT;
3505 case FCMP_OGE: return FCMP_OLE;
3506 case FCMP_OLE: return FCMP_OGE;
3507 case FCMP_UGT: return FCMP_ULT;
3508 case FCMP_ULT: return FCMP_UGT;
3509 case FCMP_UGE: return FCMP_ULE;
3510 case FCMP_ULE: return FCMP_UGE;
3511 }
3512}
3513
Sanjoy Das6e78b172015-10-22 19:57:34 +00003514CmpInst::Predicate CmpInst::getSignedPredicate(Predicate pred) {
3515 assert(CmpInst::isUnsigned(pred) && "Call only with signed predicates!");
3516
3517 switch (pred) {
3518 default:
3519 llvm_unreachable("Unknown predicate!");
3520 case CmpInst::ICMP_ULT:
3521 return CmpInst::ICMP_SLT;
3522 case CmpInst::ICMP_ULE:
3523 return CmpInst::ICMP_SLE;
3524 case CmpInst::ICMP_UGT:
3525 return CmpInst::ICMP_SGT;
3526 case CmpInst::ICMP_UGE:
3527 return CmpInst::ICMP_SGE;
3528 }
3529}
3530
Craig Topper1c3f2832015-12-15 06:11:33 +00003531bool CmpInst::isUnsigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003532 switch (predicate) {
3533 default: return false;
3534 case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT:
3535 case ICmpInst::ICMP_UGE: return true;
3536 }
3537}
3538
Craig Topper1c3f2832015-12-15 06:11:33 +00003539bool CmpInst::isSigned(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003540 switch (predicate) {
3541 default: return false;
3542 case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT:
3543 case ICmpInst::ICMP_SGE: return true;
3544 }
3545}
3546
Craig Topper1c3f2832015-12-15 06:11:33 +00003547bool CmpInst::isOrdered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003548 switch (predicate) {
3549 default: return false;
3550 case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT:
3551 case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE:
3552 case FCmpInst::FCMP_ORD: return true;
3553 }
3554}
3555
Craig Topper1c3f2832015-12-15 06:11:33 +00003556bool CmpInst::isUnordered(Predicate predicate) {
Reid Spencer266e42b2006-12-23 06:05:41 +00003557 switch (predicate) {
3558 default: return false;
3559 case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT:
3560 case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE:
3561 case FCmpInst::FCMP_UNO: return true;
3562 }
3563}
3564
Craig Topper1c3f2832015-12-15 06:11:33 +00003565bool CmpInst::isTrueWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003566 switch(predicate) {
3567 default: return false;
3568 case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE:
3569 case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true;
3570 }
3571}
3572
Craig Topper1c3f2832015-12-15 06:11:33 +00003573bool CmpInst::isFalseWhenEqual(Predicate predicate) {
Nick Lewycky7494b3b2009-10-25 03:50:03 +00003574 switch(predicate) {
3575 case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT:
3576 case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true;
3577 default: return false;
3578 }
3579}
3580
3581
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003582//===----------------------------------------------------------------------===//
3583// SwitchInst Implementation
3584//===----------------------------------------------------------------------===//
3585
Chris Lattnerbaf00152010-11-17 05:41:46 +00003586void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumReserved) {
3587 assert(Value && Default && NumReserved);
3588 ReservedSpace = NumReserved;
Pete Cooperb4eede22015-06-12 17:48:10 +00003589 setNumHungOffUseOperands(2);
Pete Cooper3fc30402015-06-10 22:38:46 +00003590 allocHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003591
Pete Coopereb31b682015-05-21 22:48:54 +00003592 Op<0>() = Value;
3593 Op<1>() = Default;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003594}
3595
Chris Lattner2195fc42007-02-24 00:55:48 +00003596/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3597/// switch on and a default destination. The number of additional cases can
3598/// be specified here to make memory allocation more efficient. This
3599/// constructor can also autoinsert before another instruction.
3600SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3601 Instruction *InsertBefore)
Owen Anderson55f1c092009-08-13 21:58:54 +00003602 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003603 nullptr, 0, InsertBefore) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003604 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003605}
3606
3607/// SwitchInst ctor - Create a new switch instruction, specifying a value to
3608/// switch on and a default destination. The number of additional cases can
3609/// be specified here to make memory allocation more efficient. This
3610/// constructor also autoinserts at the end of the specified BasicBlock.
3611SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3612 BasicBlock *InsertAtEnd)
Owen Anderson55f1c092009-08-13 21:58:54 +00003613 : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch,
Craig Topperc6207612014-04-09 06:08:46 +00003614 nullptr, 0, InsertAtEnd) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003615 init(Value, Default, 2+NumCases*2);
Chris Lattner2195fc42007-02-24 00:55:48 +00003616}
3617
Misha Brukmanb1c93172005-04-21 23:48:37 +00003618SwitchInst::SwitchInst(const SwitchInst &SI)
Craig Topperc6207612014-04-09 06:08:46 +00003619 : TerminatorInst(SI.getType(), Instruction::Switch, nullptr, 0) {
Chris Lattnerbaf00152010-11-17 05:41:46 +00003620 init(SI.getCondition(), SI.getDefaultDest(), SI.getNumOperands());
Pete Cooperb4eede22015-06-12 17:48:10 +00003621 setNumHungOffUseOperands(SI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003622 Use *OL = getOperandList();
3623 const Use *InOL = SI.getOperandList();
Chris Lattnerbaf00152010-11-17 05:41:46 +00003624 for (unsigned i = 2, E = SI.getNumOperands(); i != E; i += 2) {
Gabor Greif2d3024d2008-05-26 21:33:52 +00003625 OL[i] = InOL[i];
3626 OL[i+1] = InOL[i+1];
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003627 }
Dan Gohmanc8a27f22009-08-25 22:11:20 +00003628 SubclassOptionalData = SI.SubclassOptionalData;
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003629}
3630
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003631
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003632/// addCase - Add an entry to the switch instruction...
3633///
Chris Lattner47ac1872005-02-24 05:32:09 +00003634void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003635 unsigned NewCaseIdx = getNumCases();
3636 unsigned OpNo = getNumOperands();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003637 if (OpNo+2 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003638 growOperands(); // Get more space!
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003639 // Initialize some new operands.
Chris Lattnerf711f8d2005-01-29 01:05:12 +00003640 assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003641 setNumHungOffUseOperands(OpNo+2);
Bob Wilsone4077362013-09-09 19:14:35 +00003642 CaseIt Case(this, NewCaseIdx);
3643 Case.setValue(OnVal);
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003644 Case.setSuccessor(Dest);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003645}
3646
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003647/// removeCase - This method removes the specified case and its successor
3648/// from the switch instruction.
Bob Wilsone4077362013-09-09 19:14:35 +00003649void SwitchInst::removeCase(CaseIt i) {
Stepan Dyatkovskiy5b648af2012-03-08 07:06:20 +00003650 unsigned idx = i.getCaseIndex();
3651
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003652 assert(2 + idx*2 < getNumOperands() && "Case index out of range!!!");
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003653
3654 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003655 Use *OL = getOperandList();
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003656
Jay Foad14277722011-02-01 09:22:34 +00003657 // Overwrite this case with the end of the list.
Stepan Dyatkovskiy513aaa52012-02-01 07:49:51 +00003658 if (2 + (idx + 1) * 2 != NumOps) {
3659 OL[2 + idx * 2] = OL[NumOps - 2];
3660 OL[2 + idx * 2 + 1] = OL[NumOps - 1];
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003661 }
3662
3663 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003664 OL[NumOps-2].set(nullptr);
3665 OL[NumOps-2+1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003666 setNumHungOffUseOperands(NumOps-2);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003667}
3668
Jay Foade98f29d2011-04-01 08:00:58 +00003669/// growOperands - grow operands - This grows the operand list in response
3670/// to a push_back style of operation. This grows the number of ops by 3 times.
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003671///
Jay Foade98f29d2011-04-01 08:00:58 +00003672void SwitchInst::growOperands() {
Gabor Greiff6caff662008-05-10 08:32:32 +00003673 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003674 unsigned NumOps = e*3;
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003675
3676 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003677 growHungoffUses(ReservedSpace);
Chris Lattnerafdb3de2005-01-29 00:35:16 +00003678}
3679
3680
3681BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
3682 return getSuccessor(idx);
3683}
3684unsigned SwitchInst::getNumSuccessorsV() const {
3685 return getNumSuccessors();
3686}
3687void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
3688 setSuccessor(idx, B);
Alkis Evlogimenos93a7c062004-07-29 12:33:25 +00003689}
Chris Lattnerf22be932004-10-15 23:52:53 +00003690
Chris Lattner3ed871f2009-10-27 19:13:16 +00003691//===----------------------------------------------------------------------===//
Jay Foadbbb91f22011-01-16 15:30:52 +00003692// IndirectBrInst Implementation
Chris Lattner3ed871f2009-10-27 19:13:16 +00003693//===----------------------------------------------------------------------===//
3694
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003695void IndirectBrInst::init(Value *Address, unsigned NumDests) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003696 assert(Address && Address->getType()->isPointerTy() &&
Chris Lattner6747b4c2009-10-29 05:53:32 +00003697 "Address of indirectbr must be a pointer");
Chris Lattner3ed871f2009-10-27 19:13:16 +00003698 ReservedSpace = 1+NumDests;
Pete Cooperb4eede22015-06-12 17:48:10 +00003699 setNumHungOffUseOperands(1);
Pete Cooper3fc30402015-06-10 22:38:46 +00003700 allocHungoffUses(ReservedSpace);
3701
Pete Coopereb31b682015-05-21 22:48:54 +00003702 Op<0>() = Address;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003703}
3704
3705
Jay Foade98f29d2011-04-01 08:00:58 +00003706/// growOperands - grow operands - This grows the operand list in response
3707/// to a push_back style of operation. This grows the number of ops by 2 times.
Chris Lattner3ed871f2009-10-27 19:13:16 +00003708///
Jay Foade98f29d2011-04-01 08:00:58 +00003709void IndirectBrInst::growOperands() {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003710 unsigned e = getNumOperands();
Jay Foade98f29d2011-04-01 08:00:58 +00003711 unsigned NumOps = e*2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003712
3713 ReservedSpace = NumOps;
Pete Cooper93f9ff52015-06-10 22:38:41 +00003714 growHungoffUses(ReservedSpace);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003715}
3716
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003717IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3718 Instruction *InsertBefore)
3719: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003720 nullptr, 0, InsertBefore) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003721 init(Address, NumCases);
3722}
3723
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003724IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases,
3725 BasicBlock *InsertAtEnd)
3726: TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr,
Craig Topperc6207612014-04-09 06:08:46 +00003727 nullptr, 0, InsertAtEnd) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003728 init(Address, NumCases);
3729}
3730
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003731IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI)
Pete Cooper3fc30402015-06-10 22:38:46 +00003732 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr,
3733 nullptr, IBI.getNumOperands()) {
3734 allocHungoffUses(IBI.getNumOperands());
Pete Cooper74510a42015-06-12 17:48:05 +00003735 Use *OL = getOperandList();
3736 const Use *InOL = IBI.getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003737 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
3738 OL[i] = InOL[i];
3739 SubclassOptionalData = IBI.SubclassOptionalData;
3740}
3741
Chris Lattner3ed871f2009-10-27 19:13:16 +00003742/// addDestination - Add a destination.
3743///
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003744void IndirectBrInst::addDestination(BasicBlock *DestBB) {
Pete Cooperb4eede22015-06-12 17:48:10 +00003745 unsigned OpNo = getNumOperands();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003746 if (OpNo+1 > ReservedSpace)
Jay Foade98f29d2011-04-01 08:00:58 +00003747 growOperands(); // Get more space!
Chris Lattner3ed871f2009-10-27 19:13:16 +00003748 // Initialize some new operands.
3749 assert(OpNo < ReservedSpace && "Growing didn't work!");
Pete Cooperb4eede22015-06-12 17:48:10 +00003750 setNumHungOffUseOperands(OpNo+1);
Pete Cooper74510a42015-06-12 17:48:05 +00003751 getOperandList()[OpNo] = DestBB;
Chris Lattner3ed871f2009-10-27 19:13:16 +00003752}
3753
3754/// removeDestination - This method removes the specified successor from the
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003755/// indirectbr instruction.
3756void IndirectBrInst::removeDestination(unsigned idx) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003757 assert(idx < getNumOperands()-1 && "Successor index out of range!");
3758
3759 unsigned NumOps = getNumOperands();
Pete Cooper74510a42015-06-12 17:48:05 +00003760 Use *OL = getOperandList();
Chris Lattner3ed871f2009-10-27 19:13:16 +00003761
3762 // Replace this value with the last one.
3763 OL[idx+1] = OL[NumOps-1];
3764
3765 // Nuke the last value.
Craig Topperc6207612014-04-09 06:08:46 +00003766 OL[NumOps-1].set(nullptr);
Pete Cooperb4eede22015-06-12 17:48:10 +00003767 setNumHungOffUseOperands(NumOps-1);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003768}
3769
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003770BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003771 return getSuccessor(idx);
3772}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003773unsigned IndirectBrInst::getNumSuccessorsV() const {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003774 return getNumSuccessors();
3775}
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003776void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00003777 setSuccessor(idx, B);
3778}
3779
3780//===----------------------------------------------------------------------===//
Pete Cooper75403d72015-06-24 20:22:23 +00003781// cloneImpl() implementations
Chris Lattner3ed871f2009-10-27 19:13:16 +00003782//===----------------------------------------------------------------------===//
3783
Chris Lattnerf22be932004-10-15 23:52:53 +00003784// Define these methods here so vtables don't get emitted into every translation
3785// unit that uses these classes.
3786
Pete Cooper75403d72015-06-24 20:22:23 +00003787GetElementPtrInst *GetElementPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003788 return new (getNumOperands()) GetElementPtrInst(*this);
Chris Lattnerf22be932004-10-15 23:52:53 +00003789}
3790
Pete Cooper75403d72015-06-24 20:22:23 +00003791BinaryOperator *BinaryOperator::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003792 return Create(getOpcode(), Op<0>(), Op<1>());
Chris Lattnerf22be932004-10-15 23:52:53 +00003793}
3794
Pete Cooper75403d72015-06-24 20:22:23 +00003795FCmpInst *FCmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003796 return new FCmpInst(getPredicate(), Op<0>(), Op<1>());
Reid Spencerd9436b62006-11-20 01:22:35 +00003797}
3798
Pete Cooper75403d72015-06-24 20:22:23 +00003799ICmpInst *ICmpInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003800 return new ICmpInst(getPredicate(), Op<0>(), Op<1>());
Dan Gohman0752bff2008-05-23 00:36:11 +00003801}
3802
Pete Cooper75403d72015-06-24 20:22:23 +00003803ExtractValueInst *ExtractValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003804 return new ExtractValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003805}
3806
Pete Cooper75403d72015-06-24 20:22:23 +00003807InsertValueInst *InsertValueInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003808 return new InsertValueInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003809}
3810
Pete Cooper75403d72015-06-24 20:22:23 +00003811AllocaInst *AllocaInst::cloneImpl() const {
David Majnemer6b3244c2014-04-30 16:12:21 +00003812 AllocaInst *Result = new AllocaInst(getAllocatedType(),
3813 (Value *)getOperand(0), getAlignment());
3814 Result->setUsedWithInAlloca(isUsedWithInAlloca());
3815 return Result;
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003816}
3817
Pete Cooper75403d72015-06-24 20:22:23 +00003818LoadInst *LoadInst::cloneImpl() const {
Eli Friedman59b66882011-08-09 23:02:53 +00003819 return new LoadInst(getOperand(0), Twine(), isVolatile(),
3820 getAlignment(), getOrdering(), getSynchScope());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003821}
3822
Pete Cooper75403d72015-06-24 20:22:23 +00003823StoreInst *StoreInst::cloneImpl() const {
Eli Friedmancad9f2a2011-08-10 17:39:11 +00003824 return new StoreInst(getOperand(0), getOperand(1), isVolatile(),
Eli Friedman59b66882011-08-09 23:02:53 +00003825 getAlignment(), getOrdering(), getSynchScope());
3826
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003827}
3828
Pete Cooper75403d72015-06-24 20:22:23 +00003829AtomicCmpXchgInst *AtomicCmpXchgInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003830 AtomicCmpXchgInst *Result =
3831 new AtomicCmpXchgInst(getOperand(0), getOperand(1), getOperand(2),
Tim Northovere94a5182014-03-11 10:48:52 +00003832 getSuccessOrdering(), getFailureOrdering(),
3833 getSynchScope());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003834 Result->setVolatile(isVolatile());
Tim Northover420a2162014-06-13 14:24:07 +00003835 Result->setWeak(isWeak());
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003836 return Result;
3837}
3838
Pete Cooper75403d72015-06-24 20:22:23 +00003839AtomicRMWInst *AtomicRMWInst::cloneImpl() const {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00003840 AtomicRMWInst *Result =
3841 new AtomicRMWInst(getOperation(),getOperand(0), getOperand(1),
3842 getOrdering(), getSynchScope());
3843 Result->setVolatile(isVolatile());
3844 return Result;
3845}
3846
Pete Cooper75403d72015-06-24 20:22:23 +00003847FenceInst *FenceInst::cloneImpl() const {
Eli Friedmanfee02c62011-07-25 23:16:38 +00003848 return new FenceInst(getContext(), getOrdering(), getSynchScope());
3849}
3850
Pete Cooper75403d72015-06-24 20:22:23 +00003851TruncInst *TruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003852 return new TruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003853}
3854
Pete Cooper75403d72015-06-24 20:22:23 +00003855ZExtInst *ZExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003856 return new ZExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003857}
3858
Pete Cooper75403d72015-06-24 20:22:23 +00003859SExtInst *SExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003860 return new SExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003861}
3862
Pete Cooper75403d72015-06-24 20:22:23 +00003863FPTruncInst *FPTruncInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003864 return new FPTruncInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003865}
3866
Pete Cooper75403d72015-06-24 20:22:23 +00003867FPExtInst *FPExtInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003868 return new FPExtInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003869}
3870
Pete Cooper75403d72015-06-24 20:22:23 +00003871UIToFPInst *UIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003872 return new UIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003873}
3874
Pete Cooper75403d72015-06-24 20:22:23 +00003875SIToFPInst *SIToFPInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003876 return new SIToFPInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003877}
3878
Pete Cooper75403d72015-06-24 20:22:23 +00003879FPToUIInst *FPToUIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003880 return new FPToUIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003881}
3882
Pete Cooper75403d72015-06-24 20:22:23 +00003883FPToSIInst *FPToSIInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003884 return new FPToSIInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003885}
3886
Pete Cooper75403d72015-06-24 20:22:23 +00003887PtrToIntInst *PtrToIntInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003888 return new PtrToIntInst(getOperand(0), getType());
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003889}
3890
Pete Cooper75403d72015-06-24 20:22:23 +00003891IntToPtrInst *IntToPtrInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003892 return new IntToPtrInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003893}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003894
Pete Cooper75403d72015-06-24 20:22:23 +00003895BitCastInst *BitCastInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003896 return new BitCastInst(getOperand(0), getType());
Gabor Greif697e94c2008-05-15 10:04:30 +00003897}
Reid Spencer6c38f0b2006-11-27 01:05:10 +00003898
Pete Cooper75403d72015-06-24 20:22:23 +00003899AddrSpaceCastInst *AddrSpaceCastInst::cloneImpl() const {
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003900 return new AddrSpaceCastInst(getOperand(0), getType());
3901}
3902
Pete Cooper75403d72015-06-24 20:22:23 +00003903CallInst *CallInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003904 if (hasOperandBundles()) {
3905 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3906 return new(getNumOperands(), DescriptorBytes) CallInst(*this);
3907 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003908 return new(getNumOperands()) CallInst(*this);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003909}
3910
Pete Cooper75403d72015-06-24 20:22:23 +00003911SelectInst *SelectInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003912 return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003913}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003914
Pete Cooper75403d72015-06-24 20:22:23 +00003915VAArgInst *VAArgInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003916 return new VAArgInst(getOperand(0), getType());
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003917}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003918
Pete Cooper75403d72015-06-24 20:22:23 +00003919ExtractElementInst *ExtractElementInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003920 return ExtractElementInst::Create(getOperand(0), getOperand(1));
Chris Lattnerbbe0a422006-04-08 01:18:18 +00003921}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003922
Pete Cooper75403d72015-06-24 20:22:23 +00003923InsertElementInst *InsertElementInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003924 return InsertElementInst::Create(getOperand(0), getOperand(1), getOperand(2));
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003925}
3926
Pete Cooper75403d72015-06-24 20:22:23 +00003927ShuffleVectorInst *ShuffleVectorInst::cloneImpl() const {
Chris Lattner1dcb6542012-01-25 23:49:49 +00003928 return new ShuffleVectorInst(getOperand(0), getOperand(1), getOperand(2));
Gabor Greif697e94c2008-05-15 10:04:30 +00003929}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003930
Pete Cooper75403d72015-06-24 20:22:23 +00003931PHINode *PHINode::cloneImpl() const { return new PHINode(*this); }
Devang Patel11cf3f42009-10-27 22:16:29 +00003932
Pete Cooper75403d72015-06-24 20:22:23 +00003933LandingPadInst *LandingPadInst::cloneImpl() const {
Bill Wendlingfae14752011-08-12 20:24:12 +00003934 return new LandingPadInst(*this);
3935}
3936
Pete Cooper75403d72015-06-24 20:22:23 +00003937ReturnInst *ReturnInst::cloneImpl() const {
Devang Patel11cf3f42009-10-27 22:16:29 +00003938 return new(getNumOperands()) ReturnInst(*this);
3939}
3940
Pete Cooper75403d72015-06-24 20:22:23 +00003941BranchInst *BranchInst::cloneImpl() const {
Jay Foadd81f3c92011-01-07 20:29:02 +00003942 return new(getNumOperands()) BranchInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003943}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003944
Pete Cooper75403d72015-06-24 20:22:23 +00003945SwitchInst *SwitchInst::cloneImpl() const { return new SwitchInst(*this); }
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003946
Pete Cooper75403d72015-06-24 20:22:23 +00003947IndirectBrInst *IndirectBrInst::cloneImpl() const {
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00003948 return new IndirectBrInst(*this);
Chris Lattner3ed871f2009-10-27 19:13:16 +00003949}
3950
Pete Cooper75403d72015-06-24 20:22:23 +00003951InvokeInst *InvokeInst::cloneImpl() const {
Sanjoy Dasbd1c1bf2015-11-10 20:13:21 +00003952 if (hasOperandBundles()) {
3953 unsigned DescriptorBytes = getNumOperandBundles() * sizeof(BundleOpInfo);
3954 return new(getNumOperands(), DescriptorBytes) InvokeInst(*this);
3955 }
Devang Patel11cf3f42009-10-27 22:16:29 +00003956 return new(getNumOperands()) InvokeInst(*this);
Gabor Greif697e94c2008-05-15 10:04:30 +00003957}
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003958
Pete Cooper75403d72015-06-24 20:22:23 +00003959ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
Bill Wendlingf891bf82011-07-31 06:30:59 +00003960
David Majnemer654e1302015-07-31 17:58:14 +00003961CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
3962 return new (getNumOperands()) CleanupReturnInst(*this);
3963}
3964
David Majnemer654e1302015-07-31 17:58:14 +00003965CatchReturnInst *CatchReturnInst::cloneImpl() const {
David Majnemer0bc0eef2015-08-15 02:46:08 +00003966 return new (getNumOperands()) CatchReturnInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003967}
3968
David Majnemer8a1c45d2015-12-12 05:38:55 +00003969CatchSwitchInst *CatchSwitchInst::cloneImpl() const {
3970 return new CatchSwitchInst(*this);
3971}
3972
3973FuncletPadInst *FuncletPadInst::cloneImpl() const {
3974 return new (getNumOperands()) FuncletPadInst(*this);
David Majnemer654e1302015-07-31 17:58:14 +00003975}
3976
Pete Cooper75403d72015-06-24 20:22:23 +00003977UnreachableInst *UnreachableInst::cloneImpl() const {
Nick Lewycky42fb7452009-09-27 07:38:41 +00003978 LLVMContext &Context = getContext();
Devang Patel11cf3f42009-10-27 22:16:29 +00003979 return new UnreachableInst(Context);
Owen Anderson1e5f00e2009-07-09 23:48:35 +00003980}