llvm.org GIT mirror llvm / db07981
[GISel]: Fix undefined behavior while accessing DefaultAction map We end up dereferencing the end iterator here when the Aspect doesn't exist in the DefaultAction map. Change the API to return Optional<LLT> and return None when not found. Also update the callers to handle the None case git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302963 91177308-0d34-0410-b5e6-96231b3b80d8 Aditya Nandakumar 3 years ago
2 changed file(s) with 13 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
144144
145145 /// Iterate the given function (typically something like doubling the width)
146146 /// on Ty until we find a legal type for this operation.
147 LLT findLegalType(const InstrAspect &Aspect,
147 Optional findLegalType(const InstrAspect &Aspect,
148148 function_ref NextType) const {
149149 LegalizeAction Action;
150150 const TypeMap &Map = Actions[Aspect.Opcode - FirstOp][Aspect.Idx];
152152 do {
153153 Ty = NextType(Ty);
154154 auto ActionIt = Map.find(Ty);
155 if (ActionIt == Map.end())
156 Action = DefaultActions.find(Aspect.Opcode)->second;
155 if (ActionIt == Map.end()) {
156 auto DefaultIt = DefaultActions.find(Aspect.Opcode);
157 if (DefaultIt == DefaultActions.end())
158 return None;
159 Action = DefaultIt->second;
160 }
157161 else
158162 Action = ActionIt->second;
159163 } while(Action != Legal);
162166
163167 /// Find what type it's actually OK to perform the given operation on, given
164168 /// the general approach we've decided to take.
165 LLT findLegalType(const InstrAspect &Aspect, LegalizeAction Action) const;
169 Optional findLegalType(const InstrAspect &Aspect, LegalizeAction Action) const;
166170
167171 std::pair findLegalAction(const InstrAspect &Aspect,
168172 LegalizeAction Action) const {
169 return std::make_pair(Action, findLegalType(Aspect, Action));
173 auto LegalType = findLegalType(Aspect, Action);
174 if (!LegalType)
175 return std::make_pair(LegalizeAction::Unsupported, LLT());
176 return std::make_pair(Action, *LegalType);
170177 }
171178
172179 /// Find the specified \p Aspect in the primary (explicitly set) Actions
161161 return std::get<0>(getAction(MI, MRI)) == Legal;
162162 }
163163
164 LLT LegalizerInfo::findLegalType(const InstrAspect &Aspect,
164 Optional LegalizerInfo::findLegalType(const InstrAspect &Aspect,
165165 LegalizeAction Action) const {
166166 switch(Action) {
167167 default: